Skip to main content

Right

Returns or replaces a specified number of characters from the right end of a string.

Synopsis

Right(string,length)

Right(string,length)=value

Arguments

string String expression from which the rightmost characters are returned.
length Numeric expression that evaluates to an integer indicating how many characters from the end of string to return or replace. Fractional numbers are truncated to an integer. If length is greater than or equal to the number of characters in string, the entire string is returned (or replaced). No padding is performed. For length=0, see below.
value An expression that evaluates to a string. Specifies the value used to either replace or append. If length is 0, value is appended to the end of string. If length is greater than zero, value replaces the specified character(s) at the end of string.

Description

The Right function can be used in three ways:

  • To return a substring from the end (right end) of string. This uses the Right(string,length) syntax.

  • To replace a substring from the end (right end) of string. The replacement substring may be the same length, longer, or shorter than the original substring. This uses the Right(string,length)=value syntax, with value>0.

  • To append a substring to the end (right end) of string. This uses the Right(string,length)=value syntax, with value=0.

Right(string,length) returns the rightmost character(s) of string. The substring is determined by counting length characters backwards from the end (right end) of the string. If length is 0 or a negative number, Right returns the empty string (""). If you specify a length greater than the length of string, the entire string is returned.

Right(string,length)=value replaces the rightmost character(s) of string with value. The length argument specifies how many characters of string to replace by counting length characters backwards from the end (right end) of the string. If length is a negative number, string is unchanged. This is true even when string is the empty string (""). If length is greater than the length of string, string is replaced by value. If length=0, the value is appended to the end (right end) of string.

To determine the number of characters in string, use the Len function.

The Left function returns the specified number of characters from the beginning (left end) of a string. The Mid function returns the specified number of characters from a specified starting point within a string.

Examples

The following example uses the Right function to return the last seven characters of mystr, the last 99 characters (in this case, all of the characters), and the last 0 characters:

Dim mystr
mystr = "InterSystems"
Println "length 7:",Right(mystr,7)   ' Returns "Systems"
Println "length 99:",Right(mystr,99) ' Returns "InterSystems"
Println "length 0:",Right(mystr,0)   ' Returns ""

The following example uses the Right function to replace the last three characters of mystr with a seven-character string:

Dim mystr
mystr = "Interest"
PrintLn mystr
Right(mystr,3)="Systems"
PrintLn mystr

The following example deletes (replaces with the null string) the last three characters of mystr:

Dim mystr
mystr = "Interest"
PrintLn mystr
Right(mystr,3)=""
PrintLn mystr

The following example replaces all of the characters of mystr, because length is greater than the length of mystr:

Dim mystr
mystr = "Oracle"
PrintLn mystr
Right(mystr,99)="InterSystems"
PrintLn mystr

The following example appends a string to mystr. To append a string, length must be equal to zero (0):

Dim mystr
mystr = "Inter"
PrintLn mystr
Right(mystr,0)="Systems"
PrintLn mystr

The following example shows that a length less than 0 has no effect on mystr:

Dim mystr
Dim empstr
mystr = "InterSystems"
empstr = ""
PrintLn mystr
Right(mystr,-1)="Bongo"
PrintLn "string out:",mystr
Right(empstr,-1)="BongoSystems"
PrintLn "string out:",empstr

See Also

FeedbackOpens in a new tab