Vibin Jith
Vibin Jith

Reputation: 931

What's wrong with this VB.NET Substring call?

 dim dataType as String
  toolTip="Marks And Number[String]"

I want to get the [String] alone.

  dataType = toolTipText.Substring(toolTipText.IndexOf("[") + 1, toolTipText.IndexOf("]") - 1)

it shows an error. Regarding the length of the string.

What's wrong with my code.I dont know , Some times I have these type of problems . Standing with simple loops or conditions .

Upvotes: 0

Views: 227

Answers (2)

PaulB
PaulB

Reputation: 24372

Not that it's great code but

dataType = toolTip.Substring(toolTip.IndexOf("[") + 1, toolTip.Length - toolTip.IndexOf("[") - 2)

would sort you out.

The second parameter is the length of the substring - not the end index.

Might be better to take a look at regex.

Upvotes: 2

David M
David M

Reputation: 72850

The second parameter is length, not ending index. You need to subtract your starting index from it.

Upvotes: 4

Related Questions