Tab Split String Now Working.

Please Help. I wrote a C# program that reads code one line at a time. Each line has data seperated by tabs.

Here is what my C# code is supposed to do. The line is supposed to be split into a string array by using the "Split" string methodIt is supposed to be spit read in text one line at a time and I use the "Split" method to group the line according to the tab character.

But, as this screen shot shows, it does not work. Any idea why?

http://i67.photobucket.com/albums/h292/Athono/problem.png

Are you sure that the file actually contains tabs? It looks like the line simply contains multiple spaces, not tabs.

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?

The reason is that it might not read in the way you want.

What do you mean by that? SomeString.split('\t') will quite certainly work as expected if the given string actually contains tabs.

"John Doe\t15000\t34\tSeatle".Split('\t') returns { "John Doe", "15000", "34", "Seatle" } as expected.

 // The result will be ["John", "Doe", "15000", "34", "Seatle"]

I don't think that's the Result the OP wants. He wants to split by tabs, not spaces, and I don't think there's supposed to be a tab between "John" and "Doe" - just a regular space. In other words I assume the intended result is {"John Doe", "15000", "34", "Seatle"} and there's no way to get this result reliably if the string does not actually contain tabs (because there's no way to distinguish a space (or sequence of spaces) that was originally supposed to be a tab from a space that's just a space).

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...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.