Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split string based on the first occurrence of the character

How can I split a C# string based on the first occurrence of the specified character? Suppose I have a string with value:

101,a,b,c,d 

I want to split it as

101 
a,b,c,d 

That is by the first occurrence of comma character.

like image 353
Vishnu Y Avatar asked Feb 03 '14 04:02

Vishnu Y


People also ask

How do you split on the first occurrence?

Use the str. split() method with maxsplit set to 1 to split a string on the first occurrence, e.g. my_str. split('-', 1) . The split() method only performs a single split when the maxsplit argument is set to 1 .

How do you split a string at a certain character?

To split a string with specific character as delimiter in Java, call split() method on the string object, and pass the specific character as argument to the split() method. The method returns a String Array with the splits as elements in the array.

How do you split the first part of a string in Python?

Python Split functionPython split() method is used to split the string into chunks, and it accepts one argument called separator. A separator can be any character or a symbol. If no separators are defined, then it will split the given string and whitespace will be used by default.

How do I split a string with first space?

Using the split() Method For example, if we put the limit as n (n >0), it means that the pattern will be applied at most n-1 times. Here, we'll be using space (” “) as a regular expression to split the String on the first occurrence of space.


1 Answers

You can specify how many substrings to return using string.Split:

var pieces = myString.Split(new[] { ',' }, 2); 

Returns:

101 a,b,c,d 
like image 159
Grant Winney Avatar answered Sep 24 '22 10:09

Grant Winney