This question already has answers here:
How can I split a string with a string delimiter? [duplicate]
(7 answers)
Closed 6 years ago.
I am kinda stuck at the moment, i have a string of numbers which i dynamically get them from database, the range of numbers can be between 1 to 1 milion, something like this :
string str = "10000,68866225,77885525,3,787";
i need to create an array from it, i have tried this:
string[] strArr = { str.Replace(",", "").Split(',') };
but it doesnt work anyone has any solution i am all ours. Basically it needs to be like this:
string[] strArr = { "10000","68866225","77885525","3","787" };
Your attend:
string[] strArr = { str.Replace(",", "").Split(',') };
does not work because of two errors in the code:
1) You are removing all , before you are splitting at ,:
str.Replace(",", "")
So basically you are trying to split this string: "1000068866225778855253787" at each ,, which will result in an array containing only "1000068866225778855253787" because obviously there is no , to split on.
2) You are trying to assign an array to a string, because the Split() method already returns an array and you are trying to put this array into a field of string[] (because of the { } around your assignment) and a field of string[] is of type string and not an array.
To get your expected output you have to do the .Split(',') on the original string, which includes all the , on which you are splitting. So just remove the Replace() call and you will get your desired output:
var str = "10000,68866225,77885525,3,787";
var strArr = str.Split(',');
Try this:
string[] strArr = str.Split(',');
The split method already returns an array. Just take it out of the squiggly brackets.
string[] strArr = str.Split(',');
Edit: Sorry forgot to take out the .replace()
The method string.Split() already returns an array.
string[] strArr =yourstring.Split(',');
This will return an array with possible split counts , for example
"10000,68866225,77885525,3,787" will give array with the size of 4.
Related
This question already has answers here:
Split string requires array declaration
(2 answers)
Closed 2 years ago.
I have some code like this:
string[] separator = {"::"};
var seperatedCardString = currentCard.Name.Split(
separator, StringSplitOptions.RemoveEmptyEntries);
Can someone explain to me exactly what's happening with this and why there is a need to use {"::"}. My separator is :: so I am confused as to why it's coded the way it is.
The code line string[] separator = {"::"}; is initializing array separator. This syntax to initialize the array is referred as Implicitly Typed Arrays.
Currently your code using Split(String[], StringSplitOptions) method of string to split the string where the first arg is type of string array. If you have only one seperator (i.e. ::) then you can use the overload method Split(String, StringSplitOptions) by below code
string separator = "::";
var seperatedCardString = currentCard.Name.Split(
separator, StringSplitOptions.RemoveEmptyEntries);
Check all the overload of string Split method at here
This question already has answers here:
How do I extract text that lies between parentheses (round brackets)?
(19 answers)
Closed 7 years ago.
As I know for selecting a part of a string we use split. For example, if node1.Text is test (delete) if we choose delete
string b1 = node1.Text.Split('(')[0];
then that means we have chosen test, But if I want to choose delete from node1.Text how can I do?
Update:
Another question is that when there are two sets of parenthesis in the string, how one could aim at delete?. For example is string is test(2) (delete) - if we choose delete
You can also use regex, and then just remove the parentheses:
resultString = Regex.Match(yourString, #"\((.*?)\)").Value.
Replace("(", "").Replace(")", "");
Or better:
Regex.Match(yourString, #"\((.*?)\)").Groups[1].Value;
If you want to extract multiple strings in parentheses:
List<string> matches = new List<string>();
var result = Regex.Matches(yourString, #"\((.*?)\)");
foreach(Match x in result)
matches.Add(x.Groups[1].Value.ToString());
If your string is always xxx(yyy)zzz format, you can add ) character so split it and get the second item like;
var s = "test (delete) if we choose delete";
string b1 = s.Split(new[] { '(', ')' })[1];
string tmp = node1.Text.Split('(')[1];
string final = tmp.Split(')')[0];
Is also possible.
With the index [x] you target the part of the string before and after the character you have split the original string at. If the character occurs multiple times, your resulting string hat more parts.
I need to select a part of a string ,suppose i have a string like this :Hello::Hi,
I use this characters :: as a separator, so i need to separate Hello and Hi.I am using C# application form .
I googled it ,i found something like substring but it didn't help me.
Best regards
string.Split is the right method, but the syntax is a little tricky when splitting based on a string versus a character.
The overload to split on a string takes the input as an array of strings so it can be distinquished from the overload that takes an array of characters (since a string can be easily cast to an array of characters), and adds a parameter for StringSplitEntries, which you can set to None to use the default option (include "empty" entries):
string source = "Hello::Hi";
string[] splits = source.Split(new string[] {"::"}, StringSplitOptions.None);
You can split a string into multiple parts based on a semaphore using the Split function:
var stringToSearch = "Hello::Hi";
var foundItems = stringToSearch.Split(new[] {"::"},
StringSplitOptions.RemoveEmptyEntries);
for (int i = 0; i < foundItems.Count(); i++)
{
Console.WriteLine("Item #{0}: {1}", i + 1, foundItems[i]);
}
// Ouput:
// Item #1: Hello
// Item #2: Hi
This question already has answers here:
How do I convert a string into an array?
(7 answers)
Closed 9 years ago.
I have this string:
string var = "x1,x2,x3,x4";
I want to get x1, x2, x3, x4 separately in four different strings.
This is a basic split
string yourString = "x1,x2,x3,x4";
string[] parts = yourString.Split(',');
foreach(string s in parts)
Console.WriteLine(s);
I renamed the original variable into yourString. While it seems that it is accepted in LinqPad as a valid identifier I think that it is very confusing using this name because is reserved
Split it up.
var tokens = someString.Split(',');
Now you can access them by their index:
var firstWord = tokens[0];
String.Split
var foo = "x1,x2,x3,x4";
var result = foo.Split(',');
There's also the "opposite: String.Join
Console.WriteLine(String.Join('*', result));
Will output: x1*x2*x3*x3
To separate values with comma between them, you can use String.Split() function.
string[] words = var.Split(',');
foreach (string word in words)
{
Console.WriteLine(word);
}
This question already has answers here:
Best way to split string into lines
(12 answers)
Closed 9 years ago.
Let's say, for example, I have the following string downloaded from a .txt file in the web.
line1
line2
line3
How can I split the whole string by lines, so I can use splitted[0] to get line1, splitted[1] to get line 2, etc..? Thanks!
Can I use?
string[] tokens = Regex.Split(input, #"\r?\n|\r");
Thanks
Use File.ReadAllLines to get the string[] with all lines:
string[] allLines = File.ReadAllLines(path);
string line10 = allLines[9]; // exception if there are less
string line100 = allLines.ElementAtOrDefault(99); // null if there are less
If you already have a string you can use String.Split with Environment.NewLine
string[] textLines = text.Split(new[]{ Environment.NewLine }, StringSplitOptions.None);
Use this:
var result = Regex.Split(text, "\r\n|\r|\n");
as indicated here: Best way to split string into lines
If you are downloading a file, then open it and ReadAllLines
var f= File.ReadAllLines(filPath)
ReadAllLines returns string[].