2
123\r\n456t\r\n789

How can i split the string above in to multiple strings based on the string text .split has only over load that takes char :(

4
  • What exact output you needed can you tell this was some what unclear Commented Feb 18, 2011 at 11:12
  • 1
    Which .Net? At least in 4.0 there is an overload with string separators, I believe they were there in earlier versions. Commented Feb 18, 2011 at 11:13
  • string[] lines= logText.Split("\r\n"); I am expecting the string logText split in to multiple string Commented Feb 18, 2011 at 11:13
  • @user: But there are no newlines in your string? Commented Feb 18, 2011 at 11:14

4 Answers 4

13

string.Split has supported an overload taking an array of string delimiters since .NET 2.0. For example:

string data = "123text456text789";
string[] delimiters = { "text" };
string[] pieces = data.Split(delimiters, StringSplitOptions.None);
Sign up to request clarification or add additional context in comments.

Comments

0

use string.split("text"), hope it will help.

1 Comment

Doesn't compile.
0

I believe you want to split 123, 456, 789 as you have \r\n after them.

Easiest way I see is

string textVal = "123\r\n456t\r\n789";

textVal = textVal.replace("\r", "").replace("\n",",");

string arrVal[] = textVal.split(',');

Now you have arrVal containing 123, 456, 789.

Happy coding

Comments

0

String.Split supports also an array of strings. In your case you can do:

string s = "123\r\n456t\r\n789";
string[] parts = s.Split(new string[] {"\r\n"}, StringSplitOptions.None);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.