The sub( Command

SUB.GIF

Command Summary

Returns a specific part of a given string, or divides by 100.

Command Syntax

sub(string, start, length)

sub(value)

Menu Location

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

  1. 2nd CATALOG to enter the command catalog
  2. T to skip to command starting with T
  3. Scroll up to sub( and select it

Calculator Compatibility

TI-83/84/+/SE

Token Size

2 bytes

The sub( command is used to get a substring, or a specific part of a string. It takes three arguments: string is the source string to get the substring from, start is the index of the token to start from, and length is the length of the substring you want. For example:

:sub("TI-BASIC",4,5
    "BASIC"
:sub("TI-BASIC",5,2
    "AS"

Keep in mind that you can't have an empty string, so the length argument can't be equal to 0.

When the length argument is 1, sub(string,N,1 returns the Nth token in the string.

Advanced Uses

If only one argument is given, and it contains an expression that evaluates to a real or complex number or list of numbers, the sub( command will divide the result by 100.

:sub(225
    2.25
:sub({3+5i,-4i►Frac
    {3/100+1/20i,-1/25i}

Much like the use of the % symbol, this is an undocumented feature that was introduced in OS version 1.15. Thus, care should be taken when using sub( in this way, as older versions will not support it.


Together with the inString( command, sub( can be used to store a "list of strings" in a string, that you can then get each individual string from. To do this, think of a delimiter, such as a comma, to separate each individual string in the "list" (the delimiter must never occur in an individual string). The code will be simpler if the delimiter also occurs at the end of the string, as in "CAT,DOG,RAT,FISH,".

This routine will display each string in a "list of strings". You can adapt it to your own needs.

:1→I
:inString(Str1,",→J
:While Ans
:Disp sub(Str1,I,J-I
:J+1→I
:inString(Str1,",",Ans→J
:End

This routine allows created random letters instead of random numbers.

Repeat Getkey
Disp sub("ABCDEFGHIJKLMNOPQRSTUVWXYZ", randint(1,26), 1
End

Related Commands

.

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 License