This question already has answers here:
Split string separated by multiple spaces, ignoring single spaces
(3 answers)
Closed 7 years ago.
Lets say I have a string like this:
"15 Lore ipsum"
Is there a way to split it so I got 3 strings after split ["15", "Lore", "Ipsum"]. So I want delimiter to be a space consisting of two or more spaces, I don't want one space to be delimiter...
You can use string.Split(char[], StringSplitOptions) method:
string s = "15 Lore ipsum";
string result = s.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
\s stands for "whitespace character".
http://www.w3schools.com/jsref/jsref_regexp_whitespace.asp
Try (.*?)\s+(.*?)\s+(.*?)$
https://regex101.com/r/oH8qY6/1
Related
This question already has answers here:
How to split a string by a specific character? [duplicate]
(6 answers)
Closed 10 months ago.
I want to add ' char as delimiter to my string array but I have error telling conversion string to char impossible.
I see this How to split a string by a specific character?
string ineedtosplitthis = "i want 'to' split this";
string[] splitTest = ineedtosplitthis.Split("'").ToString();
The issues is that you have the .ToString() after your statement.
Change it to this:
string ineedtosplitthis = "i want 'to' split this";
string[] splitTest = ineedtosplitthis.Split('\'');
The reason why this code doesn't work:
string[] splitTest = ineedtosplitthis.Split("'").ToString();
Is because you are converting it to a string via ToString();, while assigning it to a string[].
Split(); already returns a string[], so there is no need to convert it.
Hope this explanation helped :)
This question already has answers here:
split a string on newlines in .NET
(17 answers)
Closed 1 year ago.
I need to convert string to IEnumerable<string> by every newline character.
I just want the same functionality as File.ReadLines without loading any file, rather taking it from a previously loaded string.
String.Split can split your string and give you string[]
Usage
string someString = ...;
string[] lines = someString.Split('\n');
IEnumerable<string> linesAsEnumerable = lines;
DoSomethingWithLines(linesAsEnumerable);
You could split it according to the newline character:
String[] lines = fileContenets.Split('\n');
This question already has answers here:
How do I split a string by a multi-character delimiter in C#?
(10 answers)
Closed 4 years ago.
How can I match this string in c# using regex so it returns 4 groups per line?
1 or more digits|one or more letters|one or more letters|one ore more X-Digit(s)\n
Example:
123|ABC|ABC|X-1;X-12;X-13
123|ABC|ABC|X-1
I've tried this
\d+\|(A-Z)\|(A-Z)\|(X-)d+
Why shooting with canons at birds?=! If you could simply use the String.Split method to achieve that
string test = "123|ABC|ABC|X-1;X-12;X-13";
string [] groups = test.Split('|');
it will return an array of elements that were separated by a |
This question already has answers here:
Split a string by another string in C#
(11 answers)
Closed 6 years ago.
I have a string like this: string ip = "192.168.10.30 | SomeName".
I want to split it by the | (including the spaces. With this code it is not possible unfortunately:
string[] address = ip.Split(new char[] {'|'}, StringSplitOptions.RemoveEmptyEntries);
as this leads to "192.168.10.30 ". I know I can add .Trim() to address[0] but is that really the right approach?
Simply adding the spaces(' | ') to the search pattern gives me an
Unrecognized escape sequence
You can split by string, not by character:
var result = ip.Split(new string[] {" | "}, StringSplitOptions.RemoveEmptyEntries);
The Split method accepts character array, so you can specify the second character as well in that array. Since you ware used RemoveEmptyEntries those spaces will be removed from the final result.
Use like this :
string[] address = ip.Split(new char[] { '|',' '}, StringSplitOptions.RemoveEmptyEntries);
You will get two items in the array
"192.168.10.30" and SomeName
This might do the trick for you
string[] address = ip.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries).Select(s => s.Trim()).ToArray();
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Regular Expression to split on spaces unless in quotes
I am dealing with various strings that I need to split into an array wherever there is a space, except for if that space exists within "quotes".
So for example, I would like this:
this is "a simple" test
..to become:
[0] = this
[1] = is
[2] = "a simple"
[3] = test
Note I would like to retain the quotes surrounding the phrase, not remove them.
The regex:
".*?"|[^\s]+
Usage:
String input = #"this is ""a simple"" test";
String[] matches =
Regex.Matches(input, #""".*?""|[^\s]+").Cast<Match>().Select(m => m.Value).ToArray();