Functions > Utility Functions > Example: String Variable Manipulation
Example: String Variable Manipulation
Use concatenation, substrings and other elementary manipulations of character strings.
Strings for Filenames
1. Define input character strings a and b.
Click to copy this expression
Click to copy this expression
Click to copy this expression
2. Use the concat function, with the current working directory (CWD) variable, to build path names for file reading functions.
Click to copy this expression
Click to copy this expression
This variable name can be passed to the READFILE function, for example, or looped through a program to read sequential files from a directory.
* 
You can also use the format function to concatenate strings combined with variables.
Click to copy this expression
Click to copy this expression
Click to copy this expression
Substrings and Position
1. Use the substr function to extract the string that starts at position 3 and contains four characters.
Click to copy this expression
2. Use the strlen function to calculate the length of string c.
Click to copy this expression
3. Use the search function to locate the starting position of the file extension substring within c.
Click to copy this expression
4. Write a program to find all occurrences of a substring in a character string.
Click to copy this expression
5. Use the program to find all occurrences of "in" in the string below.
Click to copy this expression
Converting Strings to Numbers
1. Use functions rows, cols, IsString, search, concat, substr, strlen and str2num to convert strings to numbers.
Click to copy this expression
* 
Strings and numbers can be mixed in matrices. You can use lookup functions to search for strings as well as numbers.
Click to copy this expression
2. Use the program to convert the strings in matrix M to numbers.
Click to copy this expression
Click to copy this expression
This program uses the str2num to convert strings to numbers. To convert numbers to strings, use num2str.
Click to copy this expression
Click to copy this expression
3. Change the program so that it sends out an error message if the matrix contains elements that are not strings, and use it.
Click to copy this expression
Click to copy this expression
Click to copy this expression
In the second call, the functions returned the error message that was specified as a parameter of the error function.
4. Write a program to create a vector from a character string consisting of a numerical sequence. Each number is separated from the preceding number by a comma and a space.
Click to copy this expression
Click to copy this expression
Click to copy this expression
5. Use the program to convert the following character sequence to a vector.
Click to copy this expression
Click to copy this expression
ASCII Code Conversions
1. Use the str2vec function to convert a string to a vector of ASCII codes.
Click to copy this expression
Click to copy this expression
Click to copy this expression
2. Use the vec2str function to convert a vector of ASCII codes to a string.
Click to copy this expression
3. Use the str2vec and vec2str functions to perform uppercase/lowercase transformations for character strings.
a. Use the str2vec function to define the upper/lower case characters.
Click to copy this expression
Click to copy this expression
Click to copy this expression
Click to copy this expression
b. Use the str2vec and vect2str functions to define the upper/lower case variables.
Click to copy this expression
Click to copy this expression
Click to copy this expression
Click to copy this expression
c. Use the previous definitions to convert character string S to lowercase.
Click to copy this expression
Click to copy this expression
Click to copy this expression
d. Use the previous definitions to convert character string T to uppercase.
Click to copy this expression
Was this helpful?