Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
IndexOf not returning expected value
Message
From
15/01/2013 13:37:44
 
 
General information
Forum:
ASP.NET
Category:
Coding, syntax and commands
Miscellaneous
Thread ID:
01562691
Message ID:
01562730
Views:
29
>>>>Hi,
>>>>
>>>>I'm sure this is something simple but I am too close to see the problem.
>>>>
>>>>I have a string with this value (it is being read from a text file):
>>>>
>>>>string inputLine = "1,73358,"20130111","MIA","POS","BW 791","106 8750 3231 - 7 BAGS LESS""
>>>>
>>>>I want to pull out the 73358 and the 20130111 into two variables, so I did this:
>>>>
>>>>string manifestNumber = inputLine.Substring(inputLine.IndexOf(",") + 1, inputLine.IndexOf(",", 2) - (inputLine.IndexOf(",") + 1));
>>>>string manifestDate = inputLine.Substring(inputLine.IndexOf("\"") + 1, inputLine.IndexOf("\"", 2) - (inputLine.IndexOf("\"") + 1));
>>>>
>>>>manifestNumber works properly to give me 73358.
>>>>
>>>>I get an error on manifestDate that "length cannot be less than zero".
>>>>
>>>>When I check inputLine.IndexOf("\"") I get 8
>>>>and inputLine.IndexOf("\"", 2) also returns 8!
>>>>
>>>>Why should that be?
>>>
>>>Something wrong here. This wouldn't resolve to a string:
>>>string inputLine = "1,73358,"20130111","MIA","POS","BW 791","106 8750 3231 - 7 BAGS LESS""
>>
>>sorry, it passed through UT's compiler without a problem ;)
>>
>>it is a string, I read it from a text file. I just used the outer quotation marks to illustrate the point, but I guess I should hav eused single quotes.
>>
>>FWIW, I used inputLine.Split(',') to get what I needed, but I'm very interested in what I was doing wrong with IndexOf.
>
>I was going to suggest split. Maybe I'm being dense but I can't make sense of that line. Can you just paste the line directly from the C# code.....

that line doesn't exist in the code, I was just showing you what is in the text file.

1,73358,"20130111","MIA","POS","BW 791","106 8750 3231 - 7 BAGS LESS"
string inputLine = reader.ReadLine();

string manifestNumber = inputLine.Substring(inputLine.IndexOf(',') + 1, inputLine.IndexOf(',', 2) - (inputLine.IndexOf(',') + 1));
string manifestDate = inputLine.Substring(inputLine.IndexOf('\"') + 1, inputLine.IndexOf('\"', 2) - (inputLine.IndexOf('\"') + 1));
manifestNumber is the value between the first and second commas.

manifestDate is the value between the first and second quotation marks.
Frank.

Frank Cazabon
Samaan Systems Ltd.
www.samaansystems.com
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform