Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split string using backslash

Tags:

string

c#

split

I want to split a string using the backslash ('\'). However, it's not allowed - the compiler says "newline in constant". Is there a way to split using backslash?

//For example... String[] breakApart = sentence.Split('\'); //this gives an error. 
like image 368
Karim O. Avatar asked May 28 '13 03:05

Karim O.


People also ask

What character is used to escape a backslash in a split string?

Try using the escaped character '\\' instead of '\' : String[] breakApart = sentence. Split('\\');

How do you split a string?

The split() method splits a string into an array of substrings. The split() method returns the new array. The split() method does not change the original string. If (" ") is used as separator, the string is split between words.

What does split \\ do in Java?

Java split() function is used to splitting the string into the string array based on the regular expression or the given delimiter. The resultant object is an array contains the split strings.

How do you split a string with a dash in Python?

Use the str. split() method to split a string by hyphen, e.g. my_list = my_str. split('-') .


1 Answers

Try using the escaped character '\\' instead of '\':

String[] breakApart = sentence.Split('\\'); 

The backslash \ in C# is used as an escape character for special characters like quotes and apostrophes. So when you are trying to wrap the backslash with apostrophes, the backslash together with the final apostrophe is being interpreted as an escaped apostrophe.

Here is a list of character escapes available in C#.

Here is Microsoft's documentation for character literals in C#.

like image 184
Ben Reich Avatar answered Oct 09 '22 05:10

Ben Reich