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

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

Marina Levit [MVP]

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

Guest

Hi Marina,

Thank you very much.

I looked for this solution for so long...

stupid mistake by me... :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top