xHarbour Reference Documentation > Function Reference xHarbour Developers Network  

SubStr()

Extracts a substring from a character string.

Syntax

SubStr( <cString>, <nStart>, [<nCount>] ) --> cSubstring

Arguments

<cString>
This parameter is the input string to extract a substring from.
<nStart>
This is a numeric value specifying the first character of <cString> to include in the extracted string. If <nStart> is a positive number, the function extracts from the beginning, or the left side, of <cString>. If <nStart> is a negative number, the function extracts from the end, or the right side, of <cString>.
<nCount>
This is a numeric value specifying the number of characters to extract, beginning at position <nStart>. If omitted, all characters from <nStart> to the end of <cString> are extracted.

Return

The function returns the extracted character string.

Description

SubStr() is a versatile function for extracting parts of a character string beginning at the position <nStart>. It is similar to functions Left() and Right(), but can extract a substring from the middle of the input string. SubStr() is often used in conjunction with At() and RAt() to find the starting position of the substring to extract.

Info

See also:At(), HB_ATokens(), HB_RegEx(), Left(), RAt(), Right(), Str(), StrTran(), Stuff()
Category: Character functions
Source:rtl\substr.c
LIB:xhb.lib
DLL:xhbdll.dll

Example

// The example demonstrates extraction of substrings and how
// SubStr() relates to Left() and Right().

   PROCEDURE Main
      LOCAL cString := "www.xHarbour.com"

      ? SubStr( cString, 5 )           // result: xHarbour.com
      ? SubStr( cString, 5, 8 )        // result: xHarbour

      ? SubStr( cString, 1, 3 )        // result: www
      ? Left( cString, 3 )             // result: www

      ? SubStr( cString, -3, 3 )       // result: com
      ? Right( cString, 3 )            // result: com
   RETURN

Copyright © 2006-2007 xHarbour.com Inc. All rights reserved.
http://www.xHarbour.com
Created by docmaker.exe