2

I am trying to split an array of strings where each of them have "," as the delimiter. I obtained the array of strings after an earlier split using regex for the pattern of new line. The input is from a text file and here is the pattern as in the text file.

Contents of my text file

"first", "second"
"third", "fourth", "fifth"
"Sixth", "seventh"

Second text file

"Color.auto", "(current == ff) && (current == 00)"
"Color.auto", "(current == ff) ? ((Current > 0) && (current < 10))"

The code which creates an array of strings split on new line character.

StreamReader sr = new stream reader(file.txt);
String data = sr.ReadToEnd();
String pattern = @"\r\n";
String[] result = regex.split(data, pattern);

foreach(string store in result)
{
  String temp = store.split(",".ToCharArray());
}

The problem I am facing is that I am unable to split the strings on "," further using "split". I believe it is due to the array of strings which I am trying to split.

9
  • Why did you use Store instead of store? Commented Aug 28, 2013 at 15:20
  • 1
    This even doesn't need regex, just Split method of string would be OK. Commented Aug 28, 2013 at 15:22
  • It is "store" itself in my code, it's a mistake when I typed it in here Commented Aug 28, 2013 at 15:23
  • @king I want to do the split twice. Once for the newline an the other for ",". That's why I have used regex in the first case and I am trying to use split in the second Commented Aug 28, 2013 at 15:25
  • 1
    @AjitPeter You can also use Split for the first step. Commented Aug 28, 2013 at 15:26

2 Answers 2

8

Just use String.Split with the StringSplitOptions parameter:

string[] tokens = data.Split(new char[] { '\r', '\n', ',', ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);

This will take your single string and split it based on all linefeed parts as well as all commas. I threw in the space and tab character as well, though you can remove them if you don't want them.

The second parameter is so that you don't get empty tokens returned, e.g. between '\r' and '\n'.

EDIT Based on your second file, you don't want to remove whitespace, though it looks like you do probably want to remove the quotes. In that case, your line would be as follows. (I've split it into two lines just to make it look neater.)

char[] delimiters = new char[] { '\r', '\n', ',', '"' };
string[] tokens = data.Split(delimiters, StringSplitOptions.RemoveEmptyEntries);
Sign up to request clarification or add additional context in comments.

6 Comments

I tried it on a second text file which I have added right now. It alters the output as I need to check for only a single newline at end which is why I used regex.
@AjitPeter Letting us know what you need is a good way to get what you need. :-P In this case, you don't want to remove spaces or tabs, just like Timothy mentioned in his answer regarding commas. See my edit.
"@scott" you are really far sighted :-) removing " is what I was looking to do next.
@AjitPeter This answer still has problems. If the quoted strings in the file have spaces between them, you're going to get garbage whitespace tokens.
@timothy yes i agree, just checked it using the second text file i have added.
|
3
//Functional style
var tokens = File.ReadLines(file)
    .SelectMany(line => line.Split(',')) //split lines on commas
    .Select(token => token.Trim(' ')) //remove spaces around tokens
    .Select(token => token.Trim('"')); //remove quotes around tokens

//Query style
var tokens = from line in File.ReadLines(file)
             from token in line.Split(',')
             select token.Trim(' ').Trim('"');

Note this will only work if your quoted strings in the file do not contain commas. If they do, you'll want to look into using regex.

1 Comment

What is '\"'? I think you mean '"'

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.