3

I'd like to split a string with using pattern like this:

it starts and ends with '\n' and contains an arbitrary number of whitespace or '\n' in between.

Edit:

This input:

string s = "aaa\n    \nbbb\n    \nccc\n   \n   \nddd"; 

should result in an array containing

aaa
bbb
ccc
ddd
1
  • 1
    Please edit your post to provide some sample input, and the output you'd like to obtain. As written, it seems very unclear to me. Commented Nov 11, 2011 at 23:32

2 Answers 2

5

Given your example, string.Split will be much cleaner.

var vals = s.Split('\n', StringSplitOptions.RemoveEmptyEntries);
Sign up to request clarification or add additional context in comments.

1 Comment

I think this leaves array entries with spaces. Should the first parameter maybe be: new char[] { '\n', ' ' }?
3

For the one example given, the following would accomplish the specified results:

string str = "aaa\n   \nbbb\n   \nccc\n   \n   \nddd";
string[] result = Regex.Split(str, "\n\\s*");

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.