Skip to content
Property Value
Hex Value $BB0F
Categories
Localizations
  • FR: carChaine(

inString(

Overview

Returns the character position in string of the first character of substringbeginning at start.

Availability: Token available everywhere.

Syntax

inString(string,substring[,start])

Arguments

NameTypeOptional
stringstring
substringstring
startYes

Location

2nd, catalog, inString(


Description

The inString( command searches a string for occurrences of a smaller string (similar to the Find feature on a computer), and returns the first such occurrence.

The source string is the string you want to search through; the search string is the substring you want to find. inString( will return the index of the first letter of the first occurrence of the search string found, or 0 if the search string is not present. For example:

:inString("TI-BASIC","BASIC
    4
:inString("TI-BASIC","TI
    1
:inString("TI-BASIC","I
    2
:inString("TI-BASIC","ELEPHANT
    0

You can also provide the optional starting point argument, 1 by default, that will tell the command where it should start looking. If you provide a value higher than 1 here, the command will skip the beginning of the string. This can be used to find where the search string occurs past the first occurrence. For example:

:inString("TI-BASIC","I
    2
:inString("TI-BASIC","I",2
    2
:inString("TI-BASIC","I",3
    7

Advanced Uses

You can use inString( to convert a character to a number. For example:

:inString("ABCDEFGHIJKLMNOPQRSTUVWXYZ",Str1→N

Assuming Str1 is one character long and contains a capital letter, N will hold a value of 1-26 that corresponds to that letter. This value can then be stored in a real number, list, or matrix, where a character of a string couldn't be stored. To get the character value of the number, you can use the sub( command:

:sub("ABCDEFGHIJKLMNOPQRSTUVWXYZ",N,1→Str1

Using the starting point argument of inString(, you can write a routine to return all occurrences of the search string in the source string:

:0→dim(L1
:inString(Str0,Str1
:Repeat not(Ans
:Ans→L1(1+dim(L1
:inString(Str0,Str1,Ans+1
:End

If the search string is not found, this routine will return {0} in L₁. If it is found, the result will be a list of all the places the string was found.

Optimization

The inString( command can replace checking if a string is one of a number of values. Just put all the values in a string, one after the other, and try to find the string to be checked in the string of those values:

:If Str1="." or Str1=",
can be
:If inString(".,",Str1

Be careful, because if Str1 were ".," in the above example, this would also be treated like "." or ",". If this is a problem, you can separate the values you want to check for by a character you know can't be in the string:

:If Str1="HELLO" or Str1="HI
can be
:If inString("HELLO,HI",Str1

This approach assumes that a comma would never be in Str1, and words like "HELL" or "I" are also impossible. If words like these can appear in the input, the following works:

:If inString("HELLO,HI,",Str+",

(still assumes commas aren't in Str1)

Error Conditions

  • ERR:DOMAIN is thrown if starting point is not a positive integer (starting point may be longer than the length of the source string, though).

Source: parts of this page were written by the following TI|BD contributors: brt93yoda, burr, CloudVariable, ConorOBrien, DarkerLine, GoVegan, kg583, Lionel Foxcroft, Mr Dino, Trenly.

History

Calculator OS Version Description
TI-83 0.01013 Added

Last update: September 30, 2023 15:38:51
Created: February 23, 2023 23:15:01
Authors: Adrien Bertrand