base.StringBuffer.getIndexOf

Return the position of a substring.

Syntax

getIndexOf(
   substr STRING,
   start INTEGER )
  RETURNING result INTEGER
  1. substr is the substring to be found.
  2. start is the starting position.

Usage

The getIndexOf() method returns the position of a substring in the string buffer. Specify the substring and an integer specifying the position at which the search should begin. Use 1 if you want to start at the beginning of the string buffer.

The method returns zero if the substring is not found.
CALL buf.append("abcdef")
DISPLAY buf.getIndexOf("def",1) -- Shows 4
Important: When using byte length semantics, the position is expressed in bytes. When using char length semantics, the unit is characters. This is matters when using a multibyte locale such as UTF-8.

Example

This example iterates through the complete string to display the position of multiple occurrences of the same substring.

MAIN
  DEFINE b base.StringBuffer 
  DEFINE pos INTEGER
  DEFINE s STRING
  LET b = base.StringBuffer.create()
  CALL b.append("---abc-----abc--abc----")
  LET pos = 1
  LET s = "abc"
  WHILE TRUE
    LET pos = b.getIndexOf(s,pos)
    IF pos == 0 THEN
      EXIT WHILE
    END IF
    DISPLAY "Pos: ", pos
    LET pos = pos + length(s)
  END WHILE
END MAIN