Frage

Can't figure out why this won't work.

I am trying to analyse a string of variable length containing a "." somewhere inside, and then strip off the "." and all characters before it. This is called via a web service.

When debugging, it works fine until it bails out at the last line, below, with the browser message: "System.ArgumentOutOfRangeException: Index and length must refer to a location within the string. Parameter name: length "

Anyone got any idea?

Code1, below, is an input variable passed to the web service from an eform.

Dim CharNo As New Integer
CharNo = Code1.IndexOf(".")
MyCodebookValueStrip.o_Code1 = Code1.Substring(CharNo + 1, (Code1.Length - CharNo))
War es hilfreich?

Lösung

Your calculation of the lenth of the remaining string is incorrect. You have to subtract one more:

Code1.Substring(CharNo + 1, Code1.Length - CharNo - 1)

You can also just omit the second parameter, and it will get the rest of the string:

Code1.Substring(CharNo + 1)

Andere Tipps

Perhaps you could try an alternative and very simple approach?

MyCodebookValueStrip.o_Code1 = Code1.Split(".").Last()

if you're absolutely sure the string does contain a period. Otherwise, use:

MyCodebookValueStrip.o_Code1 = Code1.Split(".").LastOrDefault()

which will return you 'Nothing' if you're string doesn't contain a period.

If your string contains more than one period, you'll get the substring after the last period in the string back. But you do have scope to do otherwise, e.g.:

"StringOne.StringTwo.StringThree".Split(".").First()

will give you "StringOne".

"StringOne.StringTwo.StringThree".Split(".").Last()

will give you "StringThree".

"StringOne.StringTwo.StringThree".Split(".").Skip(1).Take(1)

will give you "StringTwo".

You'll need to reference and import System.Linq to use this stuff, which means you'll need to be using .NET 3.5 or above.

Shouldn't it be:

Code1.Substring(CharNo + 1, (Code1.Length - CharNo - 1))

Because Code1.Length - CharNo gives you an extra character.

Ex:

"abc.abcd"

You want the last 4 characters, and length - charNo will result in 5. Therefore the error.

   Dim output As String
   Dim Code1 As String = "test.txt"
   Dim charNo As Integer = Code1.IndexOf('.')
   If ((charNo <> -1) And (Code1.Length <> charNo + 1)) Then
      output = Code1.Substring(charNo, Code1.Length - charNo)
   Else
      output = ""
   End If

The above works for me flawlessly.. could it be that you're getting a -1 position from the IndexOf method?

The problem is that you are adding one to the starting index (CharNo + 1), but you don't minus one from the length. To correct it, you should have written:

Code1.Substring(CharNo + 1, (Code1.Length - CharNo - 1))

However, it's unnecessary because all you really needed to do was:

Code1.Substring(CharNo + 1)

Also, you should probably be checking if CharNo + 1 is less than the length, just in case the period was the last character in the text:

If CharNo + 1 < Code1.Length Then
    MyCodebookValueStrip.o_Code1 = Code1.Substring(CharNo + 1)
Else
    MyCodebookValueStrip.o_Code1 = ""
End If

However, if what you are trying to get is the extension from a file name, you should be using the Path class to do it right (and easier):

MyCodebookValueStrip.o_Code1 = Path.GetExtension(Code1)

Thanks everyone. I should have realised that it needed a -1. So many right answers here, I'm not sure if I can select more than one as the "accepted answer". I'll give it a try. Thanks a lot.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top