String Functions: STR 'This Is A Test ' Whos
String Functions: STR 'This Is A Test ' Whos
A MATLAB string is an array of type char. Each character is stored in two bytes of memory. A character variable is automatically created when a string is assigned to it. For example, the statement
Name str
Size 1x14
Bytes 28
This is a test.
Concatenating Strings
strcat Function strcat concatenates two or more strings horizontally, ignoring any trailing blanks but preserving blanks within the strings. This function produces the result shown below >> result result = String 1 String 2 The result is String 1String 2.Note that the trailing blanks in the first string were ignored. = strcat(string 1 ,String 2)
strvat Function strvat concatenates two or more strings vertically,automatically padding the strings to make a valid two dimensional array.This function produces the result shown below >> result = result = Long String 1 String 2 strvcat(Long String 1,String 2)
strcmpi This function is the same as strcmp, except that it ignores the case of letters (i.e., it treats a as equal to A.)
strncmp This function compares the first n characters two strings, including any leading blanks, and returns a true(1) if the characters are identical. Otherwise, it returns a false(0).
strncmpi This function is the same as strncmp, except that it ignores the case of letters. To understand these functions, consider the two strings: str1 = hello; str2 = Hello; str3 = help; String str1 and str2 are not identical, but they differ only in case of one letter. Therefore, strcmp returns false(0), while strcmpi returns true(1). >> c = strcmp(str1,str2) c = 0 >> c = strcmpi(str1,str2) c = 1 String str1 and str3 are also not identical, and both strcmp and strcmpi will return a false(0). However, the first three characters of str1 and str3 are identical, so invoking strncmp with any value up to 3 returns a true(1): >> c = strncmp(str1,str3,2) c = 1
strmatch Function strmatch is another matching function. This one looks at the beginning characters of the rows of 2-D character array and returns a list of those rows that start with the specified character sequence. The form of this function is Result = strmatch(str,array); For example, suppose that we create a 2-D character array with the function strvcat: array = strvcat(maxarray,min value,max value); Then the following statement will return the row numbers of all rows beginning with the letters max: >> result = strmatch(max,array) result = 1 3
strrep Function strrep performs the standard search-and-replace operation. It finds all occurrences of one string within another one and replaces them by a third string. The form of this function is result = strrep(str,srch,rep1) where str is the string being checked, srchis the character string to search for, and repl is the replacement character string. For example, >> test = This is a test!