JoJo
JoJo

Reputation: 4923

How Do I Use VBScript to Strip the First n Characters of a String?

How do I use VBScript to strip the first four characters of a string?

So that the first four characters are no longer part of the string.

Upvotes: 15

Views: 90908

Answers (4)

Gopu Alakrishna
Gopu Alakrishna

Reputation: 49

I request you to use the following script to strip first 4 characters of your string StringName = Mid(StringName,5)

Upvotes: 2

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200293

You have several options, some of which have already been mentioned by others:

  • Use a regular expression replacement:

    s = "abcdefghijk"
    n = 4
    
    Set re = New RegExp
    re.Pattern = "^.{" & n & "}"  'match n characters from beginning of string
    
    result = re.Replace(s, "")
    
  • Use the Mid function:

    s = "abcdefghijk"
    n = 4
    result = Mid(s, n+1)
    
  • Use the Right and Len functions:

    s = "abcdefghijk"
    n = 4
    result = Right(s, Len(s) - n)
    

Usually string operations (Mid, Right) are faster, whereas regular expression operations are more versatile.

Upvotes: 6

Marco
Marco

Reputation: 57583

You can use

MyString = Mid(First_String, 5)

Upvotes: 27

rsc
rsc

Reputation: 4434

Try this (just create sample.vbs with this content):

Option Explicit

Dim sText

sText = "aaaaString"
sText = Right(sText, Len(sText) - 4)

MsgBox(sText)

Upvotes: 2

Related Questions