Substring Functions in VBS

  • Share
  • Print this article

A substring is a set of characters in sequence, found or taken from a string variable. For example, "day" and "od" are both substrings of "today." Visual Basic Script, also known as VBScript or VBS, has several functions that let you analyze strings for the existence of substrings, or create new substring variables from existing strings.

  1. Search Functions

    • The InStr and InStrRev functions take a string and a search for the existence of a substring within it. You have to provide the string and substring search values to either function. By default, the InStr function begins the search with the first character in the string, while the InStrRev function begins the search with the last character. However, with both functions, you can specify a different starting position, beginning from their respective ends.

    Creation Functions

    • The Left, Right and Mid functions take a string and return a substring that contains a portion of the original. The Left and Right functions require you to input a string and a length for the substring. They begin creating the substring from the left and right sides of the original, respectively. The Mid function requires you to provide an original string and a character position from which to start creating the substring, beginning from the first character. You have the option to provide a length for this function, too.

    Trim Functions

    • VBS has three trim functions: Trim, LTrim and RTrim. All three remove whitespace characters from a string and return a substring. Each function only requires the string to trim as a parameter. The LTrim function removes whitespace from the left end of a string and the RTrim function does the same, but from the right side. The Trim function is a combination of the two as it removes whitespace characters from both ends.

    Replace Function

    • The replace function takes a string, searches for the existence of a substring within it, and replaces that substring with a new substring. You must provide all three values to the function. You also have the option to provide a start position from which to begin searching for the substring, starting from the left end of the main string. You can also specify how many times you want to replace the substring. By default, the function replaces all occurrences.

Related Searches

References

Comments

Related Ads

Featured
View Mobile Site