Click here to Skip to main content
15,178,226 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I am extracting a substring like this string xValue = final[6][1].Substring(0, 6);

when the value is 0.9789 it is not showing any error but if the value is some integer like 14 it is giving Index and length must refer to a location within the string exception.But how can i handle both the situations?

string[][] final = ListofSpectraImagesForAllLocation.ElementAt(index).Value;
Updated 17-Oct-14 4:45am
BillWoodruff 17-Oct-14 9:11am
To help you with this, we need to know more about the contents of the Array Item: what's in final[6][1] ? is there a way you can reliably recognize the location of the end of the data you want to read from that Item ?
ZurdoDev 17-Oct-14 9:33am
My guess is there is a much better way to do this. What is it you are actually trying to accomplish?
chandra sekhar 17-Oct-14 9:44am
In final i am getting list of strings and at the position 6,1 i am extracting the substring.For all the things i am getting some float value but for some instances i am getting integer value how can i handle when i get an integer.
ZurdoDev 17-Oct-14 9:51am
See my answer.
chandra sekhar 20-Oct-14 1:12am
Where you want me to include .ToString(#.000000)??

You can call .ToString("#.000000") to make sure it always has at least the number of decimal places you need.

However, I still suggest that parsing decimals is probably not a good idea and it would help to know what you are actually trying to do. Seems like there should be a better way.
BillWoodruff 17-Oct-14 9:54am
I think until we know what the possible formats of the entry at [6][1] is we're just making blind guesses.
ZurdoDev 17-Oct-14 9:57am
It is. I've asked OP twice now what exactly is going on the closest to an answer is that sometimes it's a float and other times it's an int. This should work for OP but I agree, sure seems like something else would be better.
your var is int change it to string
xValue = final[6][1].ToString().Substring(0, 6);

control error
int final;
          int  len = final.ToString().Length;
            if (len < 6)
                xValue = final[6][1].ToString().Substring(0, len);
                xValue = final[6][1].ToString().Substring(0, 6);
ZurdoDev 17-Oct-14 9:32am
That doesn't help fix the OPs problem.
majid torfi 17-Oct-14 9:38am
past your program in commend for understand better problem

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900