Problem with Substing - Index and length must refer to a location

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello,

I've a strange problem with substring function.
I'm geting a string from my SQL DataBase and i want to split it to two
substings.

here is what i do:

if(temp.Length>80 && temp.Length<=160)
{
line1_txt.Text=temp.Substring(0,79);
line2_txt.Text=temp.Substring(80,temp.Length-1);
}

with line1_txt i have no problem, when i'm getting to line2_txt i get the
error message:

"Index and length must refer to a location within the string."

when i'm giving in line2 the same substring values like in line1 it works.
How can i split this line into two lines?

Thanks,
Gidi.
 
As per the error message (and the .NET documentation), Substring takes a
start index and a length. Not a start index and an end index, which is what
you are trying to give it.

You could also just use the overloaded version which just takes the start
index, and returns the rest of the string (which is what you want for that
second string).
 
Hi Marina,

Thank you very much.

I looked for this solution for so long...

stupid mistake by me... :-)
 
Back
Top