Extract a substring from a string.
Namespace:
XSharp.RT
Assembly:
XSharp.RT (in XSharp.RT.dll) Version: 2.7
Syntax FUNCTION SubS(
cTarget AS USUAL := NIL,
nStart AS USUAL := NIL,
nCount AS USUAL := NIL
) AS STRING
public static string SubS(
Usual cTarget = null,
Usual nStart = null,
Usual nCount = null
)
Request Example
View SourceParameters
- cTarget (Optional)
- Type: Usual
The string from which to extract a substring. - nStart (Optional)
- Type: Usual
The starting position in <cTarget>.
If <wStart> is positive, it is relative to the leftmost character in <cTarget>.
If <nStart> is negative, it is relative to the rightmost character in <cTarget>.
If <nStart> is zero, a NULL_STRING is returned.
- nCount (Optional)
- Type: Usual
The number of characters to extract.
If omitted, the substring begins at <nStart> and continues to the end of the string.
If <nCount> is greater than the number of characters from <nStart> to the end of <cTarget>, the extra is ignored.
Return Value
Type:
String
The substring.
If the substring is not present, a NULL_STRING is returned.
Remarks
Substr() is related to the Left() and Right() functions, which extract substrings beginning with leftmost and rightmost characters in <cTarget>, respectively.
The Substr(), Right(), and Left() functions are often used with both the At() and RAt() functions to locate either the first and/or the last position of a substring before extracting it.
They are also used to display or print only a portion of a string.
Examples
These examples extract the first and last name from a variable:
1cName := "Biff Styvesent"
2? Substr(cName, 1, 4)
3? Substr(cName, 6)
4? Substr(cName, SLen(cName) + 2)
5
6? Substr(cName, -9)
7? Substr(cName, -9, 3)
This example uses Substr() with At() and RAt() to create a function to extract a file name from a file specification:
1? FileBase("c:\prg\myfile.obj")
2FUNCTION FileBase(cFile)
3 LOCAL nPos AS DWORD
4 IF (nPos := RAt("\", cFile)) != 0
5 cFile := Substr(cFile, nPos + 1)
6 ELSEIF (nPos := At(":", cFile)) != 0
7 cFile := Substr(cFile, nPos + 1)
8 ENDIF
9 RETURN cFile
See Also