In Octave GNU, A string is basically the collection of characters enclosed between double quotes (“) or single quotes (‘).
Example of strings
“This is a string”
‘This is also a string’
In Octave, there is no limit for the length of the string. i.e. It can be of any size. Character array is used to represent a string in Octave
Escape Sequence : Some double-quoted(“) string containing backslash(‘\’), change the meaning of that string constant, these types of strings are special and are used for special purposes. The backslash (‘\’) character is known as Escape Character.
Below is the list of some Escape Sequences Escape Sequence Meaning \\ \” \’ \0 \a \b \f \n \r \t \v \nnn Represents the octal value nnn, where nnn are one to three digits between 0 and 7 \xhh… Represents the hexadecimal value hh, where hh are hexadecimal digits (‘0’ through ‘9’ and either ‘A’ through ‘F’ or ‘a’ through ‘f’).
S.No. 1 Represents a literal backslash, ‘\’ 2 Represents a literal double-quote character, ‘”‘ 3 Represents a literal single-quote character, ”’ 4 Represents the null character, ASCII code 0 5 Represents the “alert” character, ASCII code 7 6 Represents a backspace, ASCII code 8 7 Represents a formfeed, ASCII code 12 8 Represents a newline, ASCII code 10 9 Represents a carriage return, ASCII code 13 10 Represents a horizontal tab, ASCII code 9 11 Represents a vertical tab, ASCII code 11 12 13
Represents the octal value nnn, where nnn are one to three digits between 0
Represents the hexadecimal value hh, where hh are hexadecimal digits (‘0’
through ‘9’ and either ‘A’ through ‘F’ or ‘a’ through ‘f’).
String creation in Octave
In the octave, a string can be generated by using the double quotes, single quotes, and
- Using Double Quotes : varA = “String”;
- Using Single Quotes : varB = ‘String’;
- Using blanks() : varC = blanks(10), create a 10 size string of blank equivalent to ” “
String concatenation in Octave
In the octave, there are two ways to concatenate strings
- Using Square Brackett ‘’ : newStr = [oldStr1 oldStr2]; or newStr = [oldStr1, oldStr2];
- Using strcat() : newStr = strcat(oldStr1, oldStr2);
String comparison in Octave
In the octave,
strcmp() is used to compare two strings
There are various versions of
- strcmp(s1, s2, n) : compares the first n characters of s1 with s2
- strcmpi(s1, s2) : case insensitive
Below is the octave code to demonstrate the above-mentioned functions and concepts
String created using " is : This is a string . String created using ' is : This is also a string . String created using blanks() is : . String Concatenation String Concatenation using [ ] is : firstlast . String Concatenation using [, ] is : firstlast . String Concatenation using strcat() is : firstlast . String Comparision Comparision using strcmp() : 0