Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find Chr(13) in a text and split them in C#

Tags:

c#

I have written two lines of code below in vb6. The code is :

d = InStr(s, data1, Chr(13), 1) ' Fine 13 keycode(Enter) form a text data.

sSplit2 = Split(g, Chr(32))     ' Split with 13 Keycode(Enter)

But I can't write above code in C#. Please help me out. How can I write the above code in C#.

like image 549
dralialadin Avatar asked Dec 03 '12 05:12

dralialadin


People also ask

How to split a string with a delimiter in C#?

In C#, Split() is a string class method. The Split() method returns an array of strings generated by splitting of original string separated by the delimiters passed as a parameter in Split() method. The delimiters can be a character or an array of characters or an array of strings.

How do you split a string into a carriage return?

Using String. NewLine , which gets the newline string defined for the current environment. Another way to split the string is using a Unicode character array. To split the string by line break, the character array should contain the CR and LF characters, i.e., carriage return \r and line feed \n .

How do I split a string into multiple characters?

To split a String with multiple characters as delimiters in C#, call Split() on the string instance and pass the delimiter characters array as argument to this method. The method returns a String array with the splits.


2 Answers

I believe you are looking for string.Split:

string str = "Test string" + (char)13 + "some other string";
string[] splitted = str.Split((char)13);

Or you can use:

string[] splitted = str.Split('\r');

For the above you will get two strings in your splitted array.

like image 135
Habib Avatar answered Oct 01 '22 00:10

Habib


the equivalnt code for sSplit2 = Split(g, Chr(32)) is

string[] sSplit2 = g.Split('\n');
like image 43
John Woo Avatar answered Oct 01 '22 00:10

John Woo