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

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);

Related

element of separator and split function

I saw this in https://learn.microsoft.com/en-us/dotnet/api/system.string.split?view=net-5.0
Each element of separator defines a separate delimiter character. If
two delimiters are adjacent, or a delimiter is found at the beginning
or end of this instance, the corresponding element in the returned
array contains Empty.
string str = "Hello.. How.. are.. you?";
string[] words = str.Split(new char[] { '.' });
foreach (string s in words)
{
MessageBox.Show(s);
}
Outputs are: Hello "" How "" are "" you?
string str = "Hello. How. are. you?";
string[] words = str.Split(new char[] { '.' });
foreach (string s in words)
{
MessageBox.Show(s);
}
Outputs are: Hello How are you?
Why does this happen?
Your split character is .:
string[] words = str.Split(new char[] { '.' });
(the parameter is an array, because you can supply multiple different characters to split on - it does not mean it splits "on an array of dots" if that was your assumption)
So .NET splits accordingly at every dot character:
Hello.. How.. are.. you?
^^ ^^ ^^
The first part is Hello, that gets terminated by the first dot.
The second part is a string of 0 length, because it gets terminated immediately by the second dot.

Is there any way to split a string when is see Uppercase followed by a lowercase without spaces between them?

I have this string:
string countries = "SpainUnited StatesItalyFrance";
I need to separate the countries. The way I can think of is to separate the word when it finds a capital letter followed by a lowercase letter.
You can add a character for splitting using Regex.Replace() and then split by it:
string countries = "SpainUnited StatesItalyFrance";
string[] result = Regex.Replace(countries, "(\\S)([A-Z][a-z])", "$1_$2")
.Split(new char[] { '_' }, StringSplitOptions.RemoveEmptyEntries);
Live Demo

Split data from multiple rows in excel using a delimiter

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.

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?

use c# split string with Multiple delimiters

I have this string
"abc,\u000Bdefgh,\u000Bjh,\u000Bkl"
And i need to split the string in c#, every time ,\u000B appears should be a new word.
I tried this:
string[] newString = myString.Split(",\u000B");
but it didnt work, how can i do this?
Change your split command to this:
string[] newString = ip.Split(new[]{",\u000B"}, StringSplitOptions.RemoveEmptyEntries);
Or use, StringSplitOptions.None if you want to preserve empty entries while splitting.
string[] newString = myString.Split(new string[] { ",\u000B" }, StringSplitOptions.None);
Works on my machine
string myString = "abc,\u000Bdefgh,\u000Bjh,\u000Bkl";
string[] a = myString.Split(new string[] { ",\u000B" }, StringSplitOptions.RemoveEmptyEntries);
You could use the short character escape notation: ",\v" instead.
Short UTF-16 Description
--------------------------------------------------------------------
\' \u0027 allow to enter a ' in a character literal, e.g. '\''
\" \u0022 allow to enter a " in a string literal, e.g. "this is the double quote (\") character"
\\ \u005c allow to enter a \ character in a character or string literal, e.g. '\\' or "this is the backslash (\\) character"
\0 \u0000 allow to enter the character with code 0
\a \u0007 alarm (usually the HW beep)
\b \u0008 back-space
\f \u000c form-feed (next page)
\n \u000a line-feed (next line)
\r \u000d carriage-return (move to the beginning of the line)
\t \u0009 (horizontal-) tab
\v \u000b vertical-tab

Categories