# String Manipulation

Introduction

1. A string in MATLAB consists of any number of characters and is contained in a single quotes.
2. Strings are vectors in which every element is a single character.
3. MATLAB also has built-in functions that are written specifically to manipulate strings.

Creating String Variables

1. A string consists of any number of characters (including, possibly, none). The following are examples of strings:
‘ ‘
‘X’
‘Cat’
‘Hello there’
‘123’
2. A substring is a subset or part of a string. For example, ‘there’ is a substring within the string ‘Hello there’.
3. Characters include letters of the alphabet, digits, punctuation marks, white space, and control characters.
4. Control characters are characters that cannot be printed, but accomplish a task.
5. White space characters include the space, tab, newline, and carriage return.
6. Leading blanks are blank spaces at the beginning of a string.
7. Trailing blanks are blank spaces at the end of a string.
8. There are several ways that string variables can be created.
9. By using an assignment statement.
10. By using ‘input’ function.

Strings as Vectors

1. Strings are treated as vectors of characters.
2. The number of characters in a string can be found by using the length function.

3. Expressions can refer to an individual element (a character within the string), or a subset of a string, or a transpose of a string.
4. A blank space in a string is a valid character within the string.

5. A character matrix can be created that consists of strings in every row.
6. The following is created as a column vector of strings, but the end result is that it is a matrix in which every element is a character.

7. With a character matrix one can refer to an individual element (a single character) or an individual row (one of the strings).
8. As rows within a matrix must always be the same length, the shorter strings must be padded with blanks so that all strings have the same length; otherwise, an error will occur.

Concatenation

1. It means to join strings together.

2. Note that the variable names (or strings) must be separated by a blank space in the brackets, but there is no space in between the strings when they are concatenated.                                                                                                                      3. The method of using the square brackets will concatenate all of the characters in the strings, including all leading and trailing blanks.
4. The strcat function, however, will remove trailing blanks (but not leading blanks) from strings before concatenating.

Customizing String

1. The blanks function will create a string consisting of n blank characters.
2. It is usually most useful to use this function when a number of blank spaces is desired in between.

3. The sprintf function will create a string in which the output is not suppressed so the value of the string variable is shown.

Removing White Space Characters

1. The deblank function will remove blank spaces (only trailing blanks) from the end of a string.

2. The strtrim function will remove both leading and trailing blanks from a string, but not blanks in the middle of the string.

Changing Case

MATLAB has two functions that convert strings to all uppercase letters, or lowercase, called upper and lower.

Comparing String

1. The function strcmp compares strings, character by character.

2. Note that for strings these functions are used to determine whether strings are equal to each other or not, not the equality operator ==.                                                                                                                                                                            3. The function strncmp compares only the first n characters in strings and ignores the rest.

4. The first two arguments are the strings to compare, and the third argument is the number of characters to compare (the value of n).

Finding Strings

1. The function strfind receives two strings as input arguments.
2. The general form is strfind(string, substring)
3. If there are no occurrences, the empty vector is returned.

Replacing Strings

1. The function strrep finds all occurrences of a substring within a string and replaces them with a new substring.
2. The format is:
strrep(string, oldsubstring, newsubstring)

Separating Strings

1. The strtok function breaks a string into two pieces.
2. The format is:
[token, rest] = strtok(string)
3. Alternate delimiters can be defined. The format
[token, rest] = strtok(string, delimeters)

Evaluating a String

The function eval is used to evaluate a string.

Example:

The “is” Function

1. The function “isspace” returns logical true for every character that is a whitespace character.
2. The “ischar” function will return logical true if the vector argument is a character vector, or logical false if not.

Conversion between String and Number

1. To convert numbers to strings there are two functions int2str for integers and num2str for real numbers (which also works with integers).

2. The function str2num does the reverse; it takes a string in which a number is stored and converts it to the type double.

Other Features

1. The sscanf function reads data from a string.
2. The strjust function justifies a string.
3. The mat2str function converts a matrix to a string.
4. The isstrprop function examines string properties.