strtok()

Searches a string for the first occurrence of any of a set of bytes.

Synopsis

int strtok(string s [, string set]);

Parameters

Parameter Description
s String to analyze
set String to search

Return value

If nothing is found, returns -1, if an error occurs returns -2.

Errors

Missing or incorrect arguments

Description

Returns the position (in Bytes) of the first occurrence of any byte (this can be a character, see note below) from the string set in the string s.

Zero is the first Byte, one is the second Byte in s, and so forth. If no byte (character) in set s is found, the function returns -1, if an error occurs, it returns -2. The parameter set is optional, the default is the string ",.:". This means, if no comparison string is indicated, s is analyzed for the first occurrence of a comma, full stop or colon.

Depending on the character type one character can have 1 or more than 1 Bytes. e.g. the Latin character "A" consists of 1 Byte, while the Chinese character "你" consists of more than one Byte.

EXAMPLE

Returns the first occurrence of the character 'a' within the sentence "This is a test, search the character 'a' ". Note that the index starts from 0.

main()
{
    int i;
    string s = "This is a test, search the character 'a' ";
    string sSet = "a";
    i= strtok (s,sSet);
    DebugN("Position of character 'a' within the sentence "This is a test,search the character 'a':",i); // returns 8.
}
main()
{
    int i;
    string s = "你好! This is a test, search the character 'a' ";
    string sSet = "a";
    i= strtok (s,sSet);
    DebugN("Position of character 'a' within the sentence "This is a test, search the character 'a':",i); // returns 18.
}

Assignment

Strings

Availability

CTRL