V
V
vee2016-11-19 16:08:40
Programming
vee, 2016-11-19 16:08:40

How to split a string into elements in C#?

Error program:

string str = "one, two, three, four, five";
string[] num = str.Split(", ");
Console.WriteLine(num[0]);
Console.ReadLine();

As far as I understand, single quotes should be used here, but why?
And why can't more than one character be used in them? After all, either a space or a comma will remain in the word.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Artem Voronov, 2016-11-19
@newross

If you carefully read MSDN Split , it turns out that an array of characters is passed to the method. Characters are separated by single quotes. What prevents you from passing an array of characters to the method?
var num = str.Split(new [] {' ', ','});

M
MrDywar Pichugin, 2016-11-19
@Dywar

var a = "one, two, three, four, five".Split( new[] { ", " }, StringSplitOptions.None );
foreach ( string b in a ) { Console.WriteLine( b ); };

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question