User Tools

Site Tools


instr

Table of Contents

instr

in string - a string processing function that returns the character position of the first occurrence of a string in another string

syntax

instr(string1, string2 [, start])

  • string1 is the string to be searched
  • string2 is the string to look for
  • start is an optional unsigned integer expression to begin the search beyond the first character of string1

details

The arguments string1 and string2 can be string variables, string functions or string literals.

The function returns an unsigned integer with one of the following values:

  • string2 is found in string1: the position at which the match is found. The first character counts as position 1.
  • string2 is not found in string1: 0
  • start is greater than length of string1: 0
  • string1 is a null string: 0
  • string2 is a null string: 0

example

This example loops through a string in search of the term 'BASIC' and prints the start position each time the term was found.

' SharpBASIC instr programming example
' ------------------------------------
incl "lib/sys.sbi";

const text = "SharpBASIC is a great BASIC programming language";

dim n: uint;

main do
  ' initialize n and loop through the text
  loop n = 0 do
    ' look for the term 'BASIC'
    n = instr(text, "BASIC", n + 1);
    ' exit the loop if no more results
    if n == 0 do
      break;
    end
    ' print result
    print(n);
  end
end
Output:

6

23

instr.txt · Last modified: 2023/06/26 00:36 by admin