The inString( Command
INSTRING.GIF

Command Summary

Finds the first occurrence of a search string in a larger string.

Command Syntax

inString(haystack, needle, starting point)

Menu Location

This command can only be found in the Catalog. Press:

  1. 2nd CATALOG to access the command catalog
  2. I to skip to command starting with I
  3. scroll down to find inString( and select it

Calculator Compatibility

TI-83/84/+/SE/CSE/CE

Token Size

2 bytes

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).

Related Commands

.

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-Noncommercial 2.5 License.