madlan
madlan

Reputation: 1397

Substring size advice

If using the following in an if statement I get an error:

If trg.Name.Substring(4, 6).ToUpper <> ("ABCDEF") Then

I get the error: "Index and length must refer to a location within the string. Parameter name: length"

I assume this is because the string (trg.name) is too small for the 4, 6 substring. What would be the correct method of working around this problem?

Thanks, madlan.

VB.net Studio 2008.

Upvotes: 0

Views: 236

Answers (3)

Stefan
Stefan

Reputation: 11509

Instr returns the index of where the searched string is first found. So If you could do this:

If InStr(trg.Name, "ABCDEF", CompareMethod.Text) - 1 <> 4 Then

With InStr you dont have to check lenght of trg.Name.

Upvotes: 1

LukeH
LukeH

Reputation: 269368

If (trg.Name.IndexOf("ABCDEF", StringComparison.OrdinalIgnoreCase) <> 4) Then

Upvotes: 2

TreDubZedd
TreDubZedd

Reputation: 2556

You should likely be checking that the length of trg.Name is at least (4+6) characters long.

Upvotes: 0

Related Questions