company logo

String :: indexOf - Find a substring

The function returns the index position of the first occurrence of the string value passed in cString in the current string by searching forward.

Comparison depends on the string coding type of the calling string. When encoding types between strings differ, a copy of cString is created and recoded to the encoding type of the current string.

When the string has been found, the function returns the absolute position (in units) of the beginning of the searched string. When the search string could not be found, the function returns -1 .

odaba::String x = "sticky question";

odaba::String y = "sti";

x.indexOf(y);              // returns 0

x.indexOf(y,1);            // returns 10

x.indexOf(y,10);           // returns 10

x.indexOf(y,11);           // returns -1

Notes:

Not implemented now: search backward for iPosition < 0.

Return value:  Position in collection ( int32  )

The position of an element in a collection is beginning with 0 for the first element.

Implementation overview

Implementation details

  1. Find string (case sensitive)
    int32 String  :: indexOf ( odaba::String &cString )

    The function searches the string passed in cString from the beginning of the current string. Comparison is case sensitive.

    • cString - Constant string object

      When iPosition exceeds the string length or when the string is empty, the the function returns -1 (lower).

  2. to list
  3. Find string
    int32 String  :: indexOf ( odaba::String &cString, bool bCaseSensitive )

    The function searches the string passed in cString from the beginning of the current string.

    When bCaseSensitive is true (default), the search is case sensitive; otherwise the search is case insensitive.

    • cString - Constant string object

      When iPosition exceeds the string length or when the string is empty, the the function returns -1 (lower).

    • bCaseSensitive - Case sensitive option

      The option indicates case sensitive data in text ( true )

  4. to list
  5. Find string at position (case sensitive)
    int32 String  :: indexOf ( odaba::String &cString, int32 iPosition, bool bCaseSensitive )

    The function searches the string passed in cString beginning at position iPosition (default is 0). When the passed position is greater than the current string length ( lengthInUnits() ) the function returns -1 (not found). When iPosition is less than zero the function throws an exception.

    When bCaseSensitive is true (default), the search is case sensitive; otherwise the search is case insensitive.

    • cString - Constant string object

      When iPosition exceeds the string length or when the string is empty, the the function returns -1 (lower).

    • iPosition - Position in collection

      The position of an element in a collection is beginning with 0 for the first element.

    • bCaseSensitive - Case sensitive option

      The option indicates case sensitive data in text ( true )

  6. to list
  7. Find string at position
    int32 String  :: indexOf ( odaba::String &cString, int32 iPosition )

    The function searches the string passed in cstring beginning at position iPosition (default is 0). When the passed position is greater than the current string length ( lengthInUnits() ) the function returns -1 (not found).

    The search is case insensitive.

    • cString - Constant string object

      When iPosition exceeds the string length or when the string is empty, the the function returns -1 (lower).

    • iPosition - Position in collection

      The position of an element in a collection is beginning with 0 for the first element.

  8. to list