0

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?

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
hakkeem
  • 185
  • 1
  • 4
  • 13
  • 1
    possible duplicate of [C#: Split string and assign result to multiple string variables](http://stackoverflow.com/questions/5266207/c-split-string-and-assign-result-to-multiple-string-variables) – Surely Aug 18 '15 at 12:00
  • 2
    How about `str.Split(',', '/', '-')`??? – Sergey Kalinichenko Aug 18 '15 at 12:02

5 Answers5

3

You can use Split(char[]) method like;

var s = "blue,rose/yellow-white";
var array = s.Split(new char[] { ',', '/', '-' });

enter image description here

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.

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
1

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);
}
NASSER
  • 5,900
  • 7
  • 38
  • 57
0

Try this

string words = "blue;yellow-white,red";
char [] delimeters = {';','-',','};
string[] splittedWords = words.Split(delimeters);
Patryk Imosa
  • 785
  • 2
  • 10
  • 31
0

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).

Niels Abildgaard
  • 2,662
  • 3
  • 24
  • 32
0

split takes params, so this is the probably the cleanest syntax

var list = "blue,rose/yellow-white";
var result = list.split(',','/','-');