I have a string (for eg:blue,rose/yellow-white).So i want to check for the symbols and need to split all these individual strings after symbols. ie after spliting i need to get,
blue
rose
yellow
white
seperatedley.how can i do this?
I have a string (for eg:blue,rose/yellow-white).So i want to check for the symbols and need to split all these individual strings after symbols. ie after spliting i need to get,
blue
rose
yellow
white
seperatedley.how can i do this?
You can use Split(char[])
method like;
var s = "blue,rose/yellow-white";
var array = s.Split(new char[] { ',', '/', '-' });
This method returns string[]
and you can access your strings with their index numbers like array[0]
, array[1]
or you can iterate them with foreach
statement.
Probably you need this
var yourString = "blue,rose/yellow-white";
var delimiters = new[] { ',', '/','-'}; //You can add more delimiters as you required
var result = yourString.Split(delimiters, StringSplitOptions.RemoveEmptyEntries);
foreach (var item in result)
{
Console.WriteLine(item);
}
Try this
string words = "blue;yellow-white,red";
char [] delimeters = {';','-',','};
string[] splittedWords = words.Split(delimeters);
If you want to split on any non-character you can use Regex.Split:
Regex.Split("blue,rose/yellow-white", @"[^\s]");
@
makes the following string a literal string (does not try to escape \s
); [^\s]
is Regex for "not a character" (aA-zZ).
split takes params, so this is the probably the cleanest syntax
var list = "blue,rose/yellow-white";
var result = list.split(',','/','-');