Right - VBScript


Overview

Right function extracts a specified number of characters from the right end of a string. It finds use in various string manipulation tasks, such as extracting file extensions, parsing data, and formatting output.

Syntax

Right(string, [length])

Parameters:

  • string: The input string from which characters are to be extracted.
  • length (optional): The number of characters to extract from the right end. If omitted, defaults to 1.

Options/Flags

None

Examples

  • Extract the last 3 characters from a string:
Dim text = "Hello World"
Dim result = Right(text, 3) ' "rld"
  • Get the file extension from a path:
Dim path = "C:\Users\Documents\example.txt"
Dim ext = Right(path, 3) ' "txt"
  • Format a number with a fixed number of decimal places:
Dim num = 123.456
Dim formatted = Right("000" & num, 3) ' "456"

Common Issues

  • Invalid length: If length is non-positive, an error will occur.
  • Insufficient characters: If the string has fewer characters than the specified length, the entire string will be returned.

Integration

  • Left Function: Can be combined with Left function to extract characters from both ends of a string.
  • InstrRev Function: Useful in conjunction with InstrRev to find the position of a substring from the right end.
  • Format Function: Can be used to format numbers and dates by extracting specific parts with Right and combining them with other formatting options.
  • Left: Extracts characters from the left end of a string.
  • Mid: Extracts characters from a specified starting position.
  • Instr: Finds the first occurrence of a substring.