use c# split string with Multiple delimiters - c#

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

Related

My Regex.Split with '\n' takes up two spaces instead of 1

I need to split my text into each word, space, and new line.
Although the words and spaces are properly working, the \n is taking up two spaces only if it's not after a word.
Example: "\nTest\nword", here, the first \n takes up two spaces while the second one takes up one.
How would I write the proper regex?
My code:
string delimiterChars = "([ \r\n])";
wordArray = Regex.Split(myTexy, delimiterChars);
For context, I am using Unity.
Input: enter image description here
Output: enter image description here
On the output of the picture: The first element is empty and the second is \n here. I don't want the empty element.
Regex.Split will always produce empty items where the matches are consecutive, or when they are at the start/end of string.
Instead, you can use a matching and extracting approach:
string delimiterChars = "[^ \r\n]+|[ \r\n]";
string[] wordArray = Regex.Matches(myTexy, delimiterChars)
.Cast<Match>()
.Select(m => m.Value)
.ToArray();
The [^ \r\n]+|[ \r\n] regex matches one or more chars other than a space, CR and LF, or a space, CR or an LF char.
You can use regular expressions to remove leading delimiter characters.
var myTexy = "\nTest\nword";
string delimiterChars = "([ \r\n])";
myTexy = Regex.Replace(myTexy, "^" + delimiterChars, "");
var wordArray = Regex.Split(myTexy, delimiterChars);
The "^" regex option says only look for these characters at the beginning of the string.
Also, just so you are aware the behavior you are seeing is intended and is documented here:
If a match is found at the beginning or the end of the input string,
an empty string is included at the beginning or the end of the
returned array.
Let me know if this is what you are looking for -
String text = "\nTest\nword";
string[] words = Regex.Split(text, #"(\n+)");
Output -
Try this :-
string myStr = "This is test text";
wordArray = myStr.Split(new char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);
Output:

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.

c# How to split a string using a back slash (double slash not working)

I'm trying to split a string using the '\'.
I've read the topic How to split using a back slash, where are a good advice to use the escaped character '\\' instead of '\' in Split method.
However if I'm using '\\', this "eating" the first symbols of my words I want to split.
Here my code:
string firstString = "one\two\three";
char a = '\\';
string[] splittedString = firstString.Split(a);
foreach (string s in splittedString)
{
Console.WriteLine(s);
}
//Output is "one wo hree"
So WHY? Where is my mistake?
You either need to escape the \ in firstString like this
string firstString = "one\\two\\three";
Or prefix it with an "#" like this
string firstString = #"one\two\three";
These might help https://blogs.msdn.microsoft.com/csharpfaq/2004/03/12/what-character-escape-sequences-are-available/ and http://www.yoda.arachsys.com/csharp/strings.html
Try to rewrite
string firstString = "one\\two\\three";

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

how to use " as character in C#

I need to use " and ' as a character in C#, however they are special characters. So when I put them as character in the string, it will give an error. The issue is that it has many " and ' so I need to find a way to allow me to use these special characters. How can I do that
Use escape sequences: "This is a double-quote: \", and this is a single quote: \'"
Although note that since the string is delimited by double quotes, the \' escape isn't necessary: "This is a double-quote: \", and this is a single quote: '"
Simple prefix your string with # for " use "" This makes newlines easy as pie:
string example = #"A string that has double quote "" and single quote ' also a new line
";
You can use escape character \
Example
var testSTring = "\"test\""
Having a single quote ' in string doesn't trouble C#, its the double quotes ", you have to escape them with backslash like:
string str = "some\"stri'''''ng";
You can also use verbatim string # in the start and then you have to escape double quotes with another double quote like:
string str = #"some""stri'ng";
Put a backslash before special chars you want to use to escape them.
Here's a list:
http://msdn.microsoft.com/en-us/library/h21280bw.aspx

Categories