Open In App

Strings in Octave GNU

Last Updated : 06 Jun, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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

S.No.

Escape Sequence

Meaning

1

\\

Represents a literal backslash, ‘\’
2

\”

Represents a literal double-quote character, ‘”‘
3

\’

Represents a literal single-quote character, ”’
4

\0

Represents the null character, ASCII code 0
5

\a

Represents the “alert” character, ASCII code 7
6

\b

Represents a backspace, ASCII code 8
7

\f

Represents a formfeed, ASCII code 12
8

\n

Represents a newline, ASCII code 10
9

\r

Represents a carriage return, ASCII code 13
10

\t

Represents a horizontal tab, ASCII code 9
11

\v

Represents a vertical tab, ASCII code 11
12

\nnn

Represents the octal value nnn, where nnn are one to three digits between 0

and 7

13

\xhh…

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 blanks()

  • 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():

  • strcmp(s1, s2, n) : compares the first n characters of s1 with s2
  • strcmpi(s1, s2) : case insensitive strcmp()

Below is the octave code to demonstrate the above-mentioned functions and concepts

MATLAB




% String creation in Octave
str1 = "This is a string";
str2 = 'This is also a string';
str3 = blanks(10);
printf("String created using \" is : %s .\n", str1);
printf("String created using \' is : %s .\n", str2);
printf("String created using blanks() is : %s .\n\n", str3);
  
% String Concatenation in Octave
display("String Concatenation")
oldStr1 = "first";
oldStr2 = "last";
newStr1 = [oldStr1 oldStr2];
newStr2 = [oldStr1, oldStr2];
newStr3 = strcat(oldStr1, oldStr2);
printf("String Concatenation using [ ] is : %s .\n", newStr1);
printf("String Concatenation using [, ] is : %s .\n", newStr2);
printf("String Concatenation using strcat() is : %s .\n\n", newStr3);
  
% String comparison in Octave
display("String Comparison")
printf("Comparison using strcmp() : ");
disp(strcmp(str1, str2));


Output:

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 Comparison
Comparison using strcmp() : 0


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads