Question What's the Problem in my Substring Function. ?

codemink

New member
Joined
Feb 13, 2012
Messages
2
Location
Chandigarh, India, India
Programming Experience
Beginner
Consider the Statement:
VB.NET:
Dim pstr As String
         FileOpen(2, pfilepath, OpenMode.Input)
        pstr = LineInput(2)
        pstr = pstr.Substring(13, pstr.Length)
        Label1.Text = pstr
        FileClose(2)

and it is Giving me the following error:
Index and length must refer to a location within the string. Parameter name: length

What should I do.?
 
Simple maths. Lets say pstr is 40 characters long.

pstr = pstr.Substring(13, pstr.Lenght) means that you are trying to make a substring starting at character 13 and ending at character 53. Yet pstr is only 40 chars long. If all you want is to start at char 13 and end at 40, just write pstr = pstr.Substring(13).
 
The first parameter(startIndex) of the Substring method defines the starting point of the substring you want to get.
And the second parameter(length) is is the number of the characters you want to take out.

So apparently the number of characters set to pstr.Length must be greater than the number of characters that exist in the string.
 
Back
Top