Reputation: 5545
I try to extrat 1,2,3
as a String from the String Data:[1,2,3]
. Doing this with the following Code gives me the Error Index and length must refer to a location within the string
it looks like I am completely blind but I do not see what is wrong. Could anyone help me?
Sub Main()
Dim name As String = "Data:[1,2,3]"
Console.Write(name.Substring(6, name.Length - 1))
Console.Read()
End Sub
Upvotes: 3
Views: 1838
Reputation: 460058
The second argument in String.Substring
is the length, so the number of characters that should be taken from the first argument's index. You should look for the brackets instead:
Dim startIndex = name.IndexOf("["c)
If startIndex >= 0 Then
Dim endIndex = name.IndexOf("]"c, startIndex)
If endIndex >= 0 Then
startIndex += 1 ' because you dont want to include the brackets
Dim data = name.Substring(startIndex, endIndex - startIndex)
End If
End If
Upvotes: 3