I don't know C# but I looked it up on the Internet to see how split() works. Anyway, I think you should use Regex class instead to do the split. The reason is that it might not read in the way you want. Though, I am not sure whether all data line will always contain data (i.e. must have first name, last name, etc).
String[] arr = Regex.Split(line, @"\s+");
// i.e.
// line = "John Doe 15000 34 Seatle"
// The result will be ["John", "Doe", "15000", "34", "Seatle"]
By the way, shouldn't this post be under C# forum instead of Computer Science?
Taywin
Posting Maven
2,633 posts since Apr 2010
Reputation Points: 275
Solved Threads: 375
Skill Endorsements: 17
What do you mean by that? SomeString.split('\t') will quite certainly work as expected if the given string actually contains tabs.
It means if a string is...
"John Doe\t15000 34\tSeatle\n".Split('\t') return {"John Doe", "15000 34", "Seatle"}
So is it a guarantee that the delimiter will always contain only a tab? I just propose a safer way to split using white space which includes tab and white space.
I don't think that's the Result the OP wants.
I know that it is not exactly the result it would be but if you are a programmer and understand the data very well, you should know how to handle this differently with the same result or better. Coding with straight forward thought may get you stuck. In this case, why would you rely on the exact format from the input? If the input format is a little bit off, your program will break. If I were to do this and I expect each line to contain only 1 record, what I would do are...
Split the line using white spaces as delimiter
If the length is what I expected (in this case, at least 5)
Iterate through each of the split line data
Anything before the first number value will be name (first, middle, and last)
The first number found is salary
If not see the second number right after the first one
Handle the exception for malformat data
Else
It will be whatever data I am expecting
Whatever left after storing the second number will be the city
Else
Handle the exception for malformat data
In this case, you should be able to handle malformat input that may occur from time to time. That's what I mean... Not straight forward thinking...
Taywin
Posting Maven
2,633 posts since Apr 2010
Reputation Points: 275
Solved Threads: 375
Skill Endorsements: 17