Chapter 7
Chapter 7
Contents:
1. Creating String Variables
2. Operations On Strings
3. The "Is" Functions For Strings
4. Converting Between String And Number Types
1. CREATING STRING VARIABLES7
Example:
1. Finding the Length of a String:
1.1. Strings as Vectors
str = 'Hello, MATLAB!’;
Strings are treated as vectors
len = length(str);
2. Accessing Individual Characters or a subset of a
string :
char1 = str(1);
char2 = str(5);
char3 = str(3:5);
1. CREATING STRING VARIABLES
Example:
1.1. Strings as Vectors 1. Creating a Character Matrix:
A character matrix can be created charMatrix = ['Hello '; 'MATLAB'; 'World ']
where each row consists of a string. % Every row must have the same number of
characters
2. Check the size of the matrix:
size(charMatrix)
3. Accessing the elements:
charMatrix(1,:);
charMatrix(2,:);
charMatrix(:,2); char13 = charMatrix(1, 3),…
2. OPERATIONS ON STRINGS
Example:
2.1. Concatenation 1. Concatenation of Strings Using Square
Brackets:
Concatenation is the process of
joining arrays together. For str1 = 'Hello, ‘;
strings or character arrays, str2 = 'World!’;
concatenation allows to concatenatedStr = [str1 str2];
combine multiple strings into 2. Concatenation of Strings Using strcat
one. MATLAB provides different function:
ways to concatenate strings, concatenatedStr = strcat(str1, str2);
including using square brackets
3. Concatenating Strings in a Matrix:
[], the strcat function.
concatenatedStr = strcat(str1; str2);
2. OPERATIONS ON STRINGS
2.2. Creating Customized Strings