element of separator and split function - c#

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.

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:

Split Function in c# not working properly

static void Main(string[] args)
{
string Var = ",A,,,B,,C";
string[] members = Var.Split(',');
foreach (string member in members)
{
Console.WriteLine(member);
}
Console.WriteLine(members.Length);
Console.ReadLine();
}
The output of the above code is
A
B
C
7
the 7 is length of the array , but my question is that when i passed ','
in parameters of split function.
so why it takes initial ',' as a space . and why it takes 2 out of 3 ',' as space after A . and why it takes 1 out of 2 ',' as space after B ?
The answer for you question is, this is not space but this is empty string
string can be empty and you are seeing this as space.
,, <- after , we have nothing, so split method adds empty string.
If you want to remove this, you have to put after ',' StringSplitOptions
Var.Split(new char [] {','}, StringSplitOptions.RemoveEmptyEntries);
doc: https://msdn.microsoft.com/pl-pl/library/tabh47cf(v=vs.110).aspx
/For this Question only the function "split()" counts. we can give any special character to split the array elements.
Note: It is not applicable for special characters like single quotations and double quotations/
string Var = "-%%A-,,$$,-B-##%-C";
string[] members = Var.Split('-',',','#','$','%');
foreach (string member in members)
{
Console.WriteLine(member);
}
Console.WriteLine(members.Length);
Console.ReadLine();
The Image shows the output console screen:
https://i.stack.imgur.com/aArY5.png
because you have a space before first ',' and because there is only one place holder between B and C?

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 break a string at each comma?

Hi guys I have a problem at hand that I can't seem to figure out, I have a string (C#) which looks like this:
string tags = "cars, motor, wheels, parts, windshield";
I need to break this string at every comma and get each word assign to a new string by itself like:
string individual_tag = "car";
I know I have to do some kind of loop here but I'm not really sure how to approach this, any help will be really appreciate it.
No loop needed. Just a call to Split():
var individualStrings = tags.Split(new string[] { ", " }, StringSplitOptions.RemoveEmptyEntries);
You can use one of String.Split methods
Split Method (Char[])
Split Method (Char[], StringSplitOptions)
Split Method (String[], StringSplitOptions)
let's try second option:
I'm giving , and space as split chars then on each those character occurrence input string will be split, but there can be empty strings in the results. we can remove them using StringSplitOptions.RemoveEmptyEntries parameter.
string[] tagArray = tags.Split(new char[]{',', ' '},
StringSplitOptions.RemoveEmptyEntries);
OR
string[] tagArray = s.Split(", ".ToCharArray(),
StringSplitOptions.RemoveEmptyEntries);
you can access each tag by:
foreach (var t in tagArray )
{
lblTags.Text = lblTags.Text + " " + t; // update lable with tag values
//System.Diagnostics.Debug.WriteLine(t); // this result can be see on your VS out put window
}
make use of Split function will do your task...
string[] s = tags.Split(',');
or
String.Split Method (Char[], StringSplitOptions)
char[] charSeparators = new char[] {',',' '};
string[] words = tags.Split(charSeparators, StringSplitOptions.RemoveEmptyEntries);
string[] words = tags.Split(',');
You are looking for the C# split() function.
string[] tags = tags.Split(',');
Edit:
string[] tag = tags.Trim().Split(new string[] { ", " }, StringSplitOptions.RemoveEmptyEntries);
You should definitely use the form supplied by Justin Niessner. There were two key differences that may be helpful depending on the input you receive:
You had spaces after your ,s so it would be best to split on ", "
StringSplitOptions.RemoveEmptyEntries will remove the empty entry that is possible in the case that you have a trailing comma.
Program that splits on spaces [C#]
using System;
class Program
{
static void Main()
{
string s = "there, is, a, cat";
string[] words = s.Split(", ".ToCharArray());
foreach (string word in words)
{
Console.WriteLine(word);
}
}
}
Output
there
is
a
cat
Reference

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