53

Is there a substring() function in VBScript similar to Java's string.substring()?

Kev
  • 118,037
  • 53
  • 300
  • 385
Carlos Blanco
  • 8,592
  • 17
  • 71
  • 101

2 Answers2

89

Yes, Mid.

Dim sub_str
sub_str = Mid(source_str, 10, 5)

The first parameter is the source string, the second is the start index, and the third is the length.

@bobobobo: Note that VBScript strings are 1-based, not 0-based. Passing 0 as an argument to Mid results in "invalid procedure call or argument Mid".

bluish
  • 26,356
  • 27
  • 122
  • 180
Tmdean
  • 9,108
  • 43
  • 51
  • 3
    FYI - The third parameter is optional, in which case it will return to the end of the string. – Spongeboy Aug 06 '15 at 05:04
  • There are also Left() and Right() functions which substring from the left or the right, respectively. I don't know if they would have performance benefits over Mid() or not. – Adam Miller Nov 22 '16 at 15:17
13

As Tmdean correctly pointed out you can use the Mid() function. The MSDN Library also has a great reference section on VBScript which you can find here:

VBScript Language Reference (MSDN Library)

Community
  • 1
  • 1
Kev
  • 118,037
  • 53
  • 300
  • 385