Name
Right, RightB Functions
Syntax
Right(string,length)
-
string Use: Required
Data Subtype: String
The string to be processed.
-
length Use: Required
Data Subtype: Long
The number of characters to return from the right of the string.
Return Value
A Variant of subtype String.
Description
Returns a string containing the rightmost
length characters of
string.
Rules at a Glance
If
lengthis 0, a zero-length string (“”) is returned.If
lengthis greater than the length ofstring,stringis returned.If
lengthis less than zero or isNull, an error is generated.If
stringcontains aNull,RightreturnsNull.
Example
The following function assumes it’s passed either a filename or a complete path and filename, and returns the filename from the end of the string:
Private Function ParseFileName(strFullPath)
Dim lngPos, lngStart
Dim strFilename
lngStart = 1
Do
lngPos = InStr(lngStart, strFullPath, "\")
If lngPos = 0 Then
strFilename = Right(strFullPath, Len(strFullPath) - lngStart + 1)
Else
lngStart = lngPos + 1
End If
Loop While lngPos > 0
ParseFileName = strFilename
End FunctionProgramming Tips & Gotchas
Use the
Lenfunction to determine the total length ofstring.When you use the
RightBfunction with byte data,lengthspecifies the number of bytes to return.
VB/VBA Differences
Because VBScript doesn’t support strong typing, it does not
support the Right$ and
RightB$ functions, which explicitly return a
datatype.
See Also
| Len, LenB Functions, Left, LeftB Functions |
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access