How to get parts of a string [duplicate] - c#

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

Related

How can I break up the text after the # I shared in the image? [duplicate]

This question already has answers here:
Split a string by another string in C#
(11 answers)
Closed 5 months ago.
I will create a parametric structure
I will take after each '#' with its extension.
For example here I need to get two texts after #
string text = "#sdvdsv0..-+mk?/!|°¬ *$%&()oO###sdfv684618awer6816";
string[] results = Regex.Split(text, "#+").Where(s => s != String.Empty).ToArray<string>();
Full example on a .net 6.0 console:
using System.Text.RegularExpressions;
string text = "#sdvdsv0..-+mk?/!|°¬ *$%&()oO###sdfv684618awer6816";
string[] results = Regex.Split(text, "#+").Where(s => s != String.Empty).ToArray<string>();
foreach (var x in results)
{
Console.WriteLine(x);
}
Output:
sdvdsv0..-+mk?/!|°¬ *$%&()oO
sdfv684618awer6816
Use "#" to match only a single # character.
Use "#+" to match at least once with the # character

How can I get the first character of a string? [duplicate]

This question already has answers here:
C#: how to get first char of a string?
(15 answers)
Closed 4 years ago.
for example, i have some string
var a = '2,,2,';
var b = ',,,'
how to get only first character,
so i will get
a = '2'
b = ','
Use the index of the character you want, so:
a[0]
For a complete answer:
char getFirstChar(string inString, char defaultChar)
{
if (string.IsNullOrEmpty(inString)) return defaultChar;
return inString[0];
}
string s = "abcdefg";
string sB = s[0];
sB = "a"
string a= "abc";
string subString = a.Substring(0,1);
Characters in Strings can be accessed in the same way as arrays.
So for example, to get the first character in a string variable name you use the expression name[0]

How to Create a string array by spliting a string? [duplicate]

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.

How do I seperate a string by new lines? [duplicate]

This question already has answers here:
split a string on newlines in .NET
(17 answers)
Closed 6 years ago.
For example,
string example = "Useless info\nI want this line in it's own string or part of a string[]\nUseless info"
How do I get only the second line?
var secondLine = example.Split('\n')[1];
Use string splitting functions:
string[] split = example.Split('\n');
Each of the array items will be a line. Then you can access the one you want by the index.
You can use this code:
string[] text=example.split("\n");
text[1] will be the second line content.
Try this. Split to newline by using \r\n or \n
string example = "Useless info\nI want this line in it's own string or part of a string[]\nUseless info";
string[] lines = example.Split(new string[]
{
"\r\n", "\n"
}
, StringSplitOptions.None);
foreach (var line in lines)
{
Console.WriteLine(line);
}

splitting the string and choosing the middle part containing two set of parenthesis [duplicate]

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.

Categories

Resources