Split data from multiple rows in excel using a delimiter - c#

I've a windows form with multiline text box. I'm trying to copy paste some data from excel sheet
I'm trying to split this values and add it to a string array using the below code
string[] languageList = language.Split('\n');
The output I'm getting is a single string
"c#javac++C"
instead of 4 strings, ie
languageList[0] = "c#"
languageList[1] = "java"
languageList[2] = "c++"
languageList[3] = "C"
Is there a way to split the excel row values using any delimiter?

Your chosen delimiter \n is the Linefeed LF character, which delimits new lines on Linux-like operating systems.
The delimiter \r\n is the Carriage Return + Linefeed CRLF character, which delimits new lines on Windows machines.
For the most robust code, use the System.Environment.NewLine property, which will pick the correct delimiter string based on the environment.
string[] languageList = language.Split(new[] { System.Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
Notice the use of new[] { System.Environment.NewLine } - this is because when using a string separator with String.Split() you must pass it as a string[] argument.

Related

C# Unable to split string by new lines \n

I'm facing a odd problem were C# is unable to split a string for new lines. I tried many combinations like use only Split.('\n') but all lead to return the whole string unsplited on first position of the array so lines[0] is the same as the input string to be splited, that never happen before with other strings i had to parse.
Image bellow:
String:
Don't remove the following keywords! These keywords are used in the
"compatible printer" condition of the print and filament profiles to
link the particular print and filament profiles to this printer
profile.\nPRINTER_VENDOR_PRUSA3D\nPRINTER_MODEL_SL1\nPRINTER_VENDOR_EPAX\nPRINTER_MODEL_X1\n\nSTART_CUSTOM_VALUES\nFLIP_XY\nLayerOffTime_0\nBottomLightOffDelay_2\nBottomLiftHeight_5\nLiftHeight_5.5\nBottomLiftSpeed_40.2\nLiftSpeed_60\nRetractSpeed_150\nBottomLightPWM_255\nLightPWM_255\nAntiAliasing_4
; Use 0 or 1 for disable AntiAliasing with "printer gamma correction"
set to 0, otherwise use multiples of 2 and "gamma correction" set to 1
for enable\nEND_CUSTOM_VALUES
Code:
var lines = previousString.Split(new[] { "\r\n", "\r", "\n" }, StringSplitOptions.RemoveEmptyEntries);
Output:
An array of lenght = 1 producing lines[0] == previousString
string[] lines = theText.Split(
new[] { Environment.NewLine },
StringSplitOptions.None
);
edit:
string[] lines = theText.Split(
new[] { "\r\n", "\r", "\n" },
StringSplitOptions.None
);
working fiddle: https://dotnetfiddle.net/HNY8a6
See: this SO post
Sometimes when you see a \n on screen it really is a backslash (ASCII 92 and an en(ASCII 110) not a placeholder/escape sequence for new line (ASCII 10) A big hint for that here is that text boxes will usually not display newlines with escape codes but will put in actual new lines.
To split on \n use the string "\\n" which represents a string of two characters: the two backslashes produce a single character ASCII 92 = '' in the string and then a lowercase n.
Alternately you could use #"\n". The # sign tells C# not to use escape codes in the quoted string.
I'm not quite sure why you are using the Printer methods but I hope you don't require them.
string test = "Hello \nTest \n123"; //Create Test String
string[] seperated = test.Split('\n'); //Splite String by '\n'
for(int i = 0; i < seperated.Length; i++){ //Output substrings
Console.WriteLine(seperated[i]);
}
Output:
Hello
Test
123
I hope this solution works for you!
Edit: Added \r\n and \r support
If you also need to split strings by '\r' or '\r\n' then this code is the one to go with.
string test = "Hello \r\nTest \n123 \rEnd"; //Create Test String
test = test.Replace("\r\n","\n");
test = test.Replace("\r","\n");
string[] seperated = test.Split('\n'); //Splite String by '\n'
for(int i = 0; i < seperated.Length; i++){ //Output substrings
Console.WriteLine(seperated[i]);
}
Output:
Hello
Test
123
End
Edit2: Hopefully Solution
So you are saying that
\nPRINTER_VENDOR_PRUSA3D\nPRINTER_MODEL_SL1\nPRINTER_VENDOR_EPAX\nPRINTER_MODEL_X1\n\nSTART_CUSTOM_VALUES\nFLIP_XY\nLayerOffTime_0\nBottomLightOffDelay_2\nBottomLiftHeight_5\nLiftHeight_5.5\nBottomLiftSpeed_40.2\nLiftSpeed_60\nRetractSpeed_150\nBottomLightPWM_255\nLightPWM_255\nAntiAliasing_4 ; Use 0 or 1 for disable AntiAliasing with "printer gamma correction" set to 0, otherwise use multiples of 2 and "gamma correction" set to 1 for enable\nEND_CUSTOM_VALUES
is the string then the problem might be that this string contains some " which will interfere with the .Split method
If you're able to input the string manually you should replace a simple " with a "

How to read in C# the Newline character in a SQLite database?

An app I'm currently working on needs to retrieve the Newline (\n) character in a TEXT Field stored in a SQLite DB. What is the corresponding character for \n in SQLite?
Because
string[] words = str.Split(new string[] { #"\r\n" }, StringSplitOptions.None);
doesn't seem to work.
string[] words = str.Split(new string[] { "\r\n" }, StringSplitOptions.None);
No # before the "", otherwise it means \ and r and \ and n... or try
string[] words = str.Split(new string[] { "\n" }, StringSplitOptions.None);
depending on what the program saved in the db.
Now, if you really want to be sure to catch anything, you could...
string[] words = str.Split(new[] { "\r\n", "\r", "\n", StringSplitOptions.None);
The # means
A verbatim string literal consists of an # character followed by a
double-quote character, zero or more characters, and a closing
double-quote character. A simple example is #"hello". In a verbatim
string literal, the characters between the delimiters are interpreted
verbatim, the only exception being a quote-escape-sequence. In
particular, simple escape sequences and hexadecimal and Unicode
escape sequences are not processed in verbatim string literals. A
verbatim string literal may span multiple lines.
You can always use Environment.NewLine.
// Summary:
// Gets the newline string defined for this environment.
//
// Returns:
// A string containing "\r\n" for non-Unix platforms, or a string containing
// "\n" for Unix platforms.
public static string NewLine { get; }
string[] words = str.Split(new[] { Environment.NewLine} , StringSplitOptions.None);

spliting string with double new line characters

I have a .txt file, the data of which I have stored in a long string. There are many single new line characters in the string after every line. And there are double new line characters at the end of paragraphs. What I want is to split the string into an array of paragraphs.
what I thought is the following but it is not working
string filePath = "C:\\Users\\Data.txt";
StreamReader readFile = new StreamReader(filePath);
string Data = readFile.ReadToEnd();
string[] paragraphss = Regex.Split(Data, "(^|[^\n])\n{2}(?!\n)");
please help
thank you
If you're OK with not using regex, Data.Split("\n\n") should do the trick.
On windows systems the newline character is \r\n, on Unix systems it is \n. This may be why the lines aren't being split, because you're specifically looking for \n\n instead of \r\n\r\n.
You can however use Environment.Newline, which will return the correct newline character for whatever environment the software is running on.
Inspired by #LueTm's answer and #Traubenfuchs' comment, just making it look compiler friendly and complete. Here's how to split a string with double new line characters:
Data.Split(new string[] { "\r\n\r\n" }, StringSplitOptions.None);

constant to split string by carriage return (CR) in c#

I am trying to split a string into two arrays.
The first array has data at the beginning of the string which is split by the \t (tab) character, and the remainder somes after the first newline character (\n).
I tried this, thinking that's what I wanted:
string[] pqRecords = pqRequests.ToString().Split('\n');
I also tried this:
internal static readonly string segment = Environment.NewLine + "\t";
string[] pqRecords = pqRequests.ToString().Split(segment);
unfortunately the Split method will only take a single character.
I know there are vbcr in my pqRequests string variable because when I mouse over it and look at the text visualize there is the first line with tabs, everything else is on it's own line.
This data is taken from a txt file, and in the file, when opened in Notepad++, I can see the CR characters.
Is there an alternative constant in c# I should use for these CR characters?
string.Split will happily accept multiple separator characters. You just have to pass them in as an array:
internal static readonly string segment = Environment.NewLine + "\t";
string[] pqRecords = pqRequests.ToString().Split(segment.ToArray());
Of course you can (and should) write the same more clearly as
internal static readonly char[] separators = new[] { '\n', '\t' };
string[] pqRecords = pqRequests.ToString().Split(separators);
The carriage return character is represented by '\r', is that what you need?

How to indicate whitespaces while reading from a .txt file

I have a simple .txt file with X,Y-values in it. It is structured like this:
-25.7754 35.87
-22.1233 32.16
-20.361 30.75
etc.
I am able to read single lines or the whole text to the end, with objstream.ReadToEnd(); & objstream.ReadLine().
But here's my question how could I indicate when the String after the first value ends so I can save/parse it to float & proceed reading the value of the next string?
Here is the read functionality I have so far :)
StreamReader objStream = new StreamReader("C:blablabla\\Text.asc");
textBox1.Text = objStream.ReadLine();
Thanks in advance,
BC++
Use String.split()
As requested, an example :
string s = "there is a cat";
//
// Split string on spaces.
// ... This will separate all the words.
//
string[] words = s.Split(' ');
foreach (string word in words)
{
Console.WriteLine(word);
}
The output is :
there
is
a
cat
Look at the string.Split methods:
var line1 = objStream.ReadLine();
var lineParts = line1.Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
textBox1.Text = lineParts[0];
textBox2.Text = lineParts[1];
Note the use of an overload that uses StringSplitOptions.RemoveEmptyEntries - the means that if you have multiple spaces in succession, the result will not contain empty entries.
If you really mean white-space and not space then you have to go this way:
string line = "-25.7754 35.87";
string[] values = line.Split(new char[] { }, StringSplitOptions.RemoveEmptyEntries);
The difference from the other answers in the splitting character. If this not defined then white-space characters are assumed to be the delimiters. In other words you will get the same result for
string line = "-25.7754\t35.87"; // tab instead of spaces.
You will have the flexibility to split correctly fixed length or tab delimited lines using the same code.

Categories