MATLAB provides a number of string processing functions, such as the following table:
function |
function |
function |
function |
Eval (String) |
Executes the string in parentheses as a matlab command |
Isspace |
Returns true if a space character exists |
Blanks (N) |
Returns a string of n 0 or a space |
Isstr |
Returns true if the input is a string |
Deblank |
Remove the space behind a string |
LastErr |
Returns the string from the previous MATLAB error |
Feval |
The function value given by the string is evaluated |
strcmp |
The string returns the True value at the same time |
Findstr |
Find a string from within a string |
Strrep |
Replace another string with one string |
Isletter |
Returns true if a letter exists |
Strtok |
Find the first tag in a string |
Let's take a look at the exact operation of the string.
First, string construction
1, the direct assignment method, writes a string in the single quotation mark, if the string itself includes the single quotation mark, then adds one single quotation mark.
str=‘dd‘‘123‘str =dd‘123
2, multi-line string if written in [], then the length of the multi-line string must be the same, if written within {}, then the length can be different.
str=[‘123‘;‘345‘]str =123345str={‘123‘;‘12345‘}str = ‘123‘ ‘12345‘
3. Using the Strcat function, Strvcat and char functions
The Strcat function connects two strings in a horizontal direction.
a=‘123‘a =123>> b=‘456‘b =456>> c=strcat(a,b)c =123456
Strvcat strings are concatenated vertically, each line of string lengths does not require equality, and the right side of all non-longest strings automatically compensates for the same length of string for each line.
a=‘123‘a =123>> b=‘1234‘b =1234>> c=strvcat(a,b)c =1231234
The char function is similar to Strvcat, but when there are empty strings in a multiline string, the Strvcat function is automatically ignored, and the CHAR function will compensate for the empty string with a space before connecting.
>> a=‘123‘;>> b=‘123‘;>> c=‘‘;>> d=‘123‘;>> e=strvcat(a,b,c,d)e =123123123>> f=char(a,b,c,d)f =123123123
Second, string comparison
You can use a relational operator directly or you can use the strcmp function, but the comparison results are different. If you use a relational operator, the length of the two strings that are required to participate in the comparison must be consistent, and the result returned is the same size as the two, and the values in the array are the result of a two-character comparison at the corresponding position, but strcmp is the comparison of two strings for equality.
>> a=‘Hello‘;>> b=‘World‘;>> a==bans = 0 0 0 1 0>> strcmp(a,b)ans = 0>> c=‘Hello‘;>> strcmp(a,c)ans = 1
Third, find and replace
You can use the FINDSTR function to find and search for strings.
str=‘Xing JIarong is so strong‘;>> findstr(str,‘ro‘)ans = 9 22
You can use the STRREP function to replace characters in a string.
>> strrep(str,‘ro‘,‘12‘is so st12ng
Attention:
- findstr function is case sensitive to letters
- The FINDSTR function has no effect on the string matrix. So the search for a string matrix can only be done by traversing the elements of the matrix, and then searching
- The direct assignment method cannot replace two strings of different lengths, and the Strrep function can replace two arbitrary-length strings, and Strrep does not work with string matrices.
MATLAB string processing