HP Prime/Commands/Guides/All About Strings

From TI-Planet's HP Calcs Wiki
Jump to: navigation, search

The string data type has a type ID of 2.

Making Constants

String constants are enclosed "like this". An unclosed string constant is always a syntax error.

You can use backslash escapes in string constants. The valid escapes are the following:

Escape code Meaning
\n Newline
\r Carriage Return
\t Tab
\\ Backslash
\" Double-quote
"" Double-quote
\0 NUL
\nnn Where 'nnn' is an octal number: The literal character with code nnn. '100', the lowest valid nnn, is @.
\Xnn Where 'nn' is an hex number: The literal character with code nn.
\unnnn Where 'nnnn' is an hex number: The literal character with code nn. The 16-bit version of \X.

Use

String variables are indexable. Getting a number from a string results in a character code, and setting it sets a character code:

LOCAL s := "Hello";
PRINT(s[1]);
s[1] := 52;

The number has to be in range, else an error is thrown. Decimals are rounded down to integers.

Getting a substring can be done by indexing a string variable with two arguments. The first one is index, and the second is the number of characters to get:

LOCAL s := "Hello";
PRINT(s[2,2]);

The second number has to be any number greater than or equal to zero.

Setting an index of a string with another string performs a limited replacement.

LOCAL s := "Hello";
s[1] := "World";

The length of the string assigned to is preserved; that is, if the replacement would make the string longer, it is truncated instead.

Limits

Strings may have at maximum 65,535 (2^16-1) characters in them. Attempting to create a longer string will result in error 38 being thrown.