The string
variable type can hold any sequence of characters. String values are
represented by the sequence of characters in double quotes (“”).
string
s = “This is a string”;
disp(“This is a string passed to a function”); |
Each character
in the string has an index that is zero-based For example, the first
character has an index of 0, the second an index of one and so on:
String: A s t r i n g . . . !
- - - - - - - - . . .
Index: 0 1 2 3 4 5 6 7 . . .n-1
These indices
are used to refer to the position within a string and are used in many
string manipulation functions.
Strings
can contain special characters that cannot be ordinarily represented
as a character, such as tabs and new lines. These values are represented
by a special escape character '\' followed by a letter describing what
kind of character is represented. Note that even though the special
character is input as two characters ('\' and the special character),
it is internally stored as one character.
Special
Character |
Description |
Syntax |
Tab |
A tab character |
\t |
New
Line |
Advances to the next line |
\n |
Carriage
Return |
Performs a carriage return |
\r |
\ |
Used when a ‘back
slash’ is needed, and not an escape character |
\\ |
" |
A quote,
for when a quote is in the string and does not signify the end
of the string. |
\" |
For example,
the following string:
"Hello,\nString\tUser,
\"Have a good day\\night !!!\"" |
is displayed
as:
Hello,
String User, "Have a good day\night !!!" |
There are
many specialized string functions and operators for manipulating strings.
This includes searching strings, comparing strings, and modifying the
contents of strings. The following table shows special operators used
on strings:
Operator |
Description |
Syntax |
+ |
Concatenates two strings. |
s1
= s2 + s3 |
== |
Equality comparison
Returns: true if both strings are identical, false otherwise
|
s1
== s2 |
!= |
Inequality comparison
Returns: true if the strings are not identical, false otherwise
|
s1
!= s2 |
> |
Greater-than comparison
Returns: true if the first string is longer in length, or if the
ASCII number representation of the string is greater than that of
the second string
|
s1
> s2 |
< |
Less-than
comparison
Returns: true if the first string is shorter in length, or if
the ASCII number representation of the string is less than that
of the second string
|
s1
< s2 |
= |
Assignment
operator. Assigns the value of the first string to that of the second |
s1
= s2 |
Note that
the comparison operators can be combined for less than or equal (<=)
or greater than or equal (>=).
1 >>string
s1 = "Hi";
2 >>string s2 = "Bye";
3 >>disp(s1 == s2);
false
4 >>disp(s1 == "Hi");
true
5 >>disp(s1 != s2);
true
6 >>disp(s1 + s2);
HiBye
7 >>disp(s1 < s2);
false
8 >>disp(s1 > s2);
true |
The next
table shows some string specific functions:
Function |
Description |
|
Finds the first occurrence
of a string within another string. |
|
Extracts a portion of
the input string and returns it. |
|
Removes leading and trailing
white-space from a string. |
|
Returns the length of
the string. |
|
Formats an int or real
into a string (similar to sscanf in C). |
|
Replaces all occurrences
in a string of one expression with another expression. |
|
Splits a string into an
array of strings based on a parsing token. |
|
Converts any variable
into its string representation. |
|
Reverses the character
order of a string |