0% found this document useful (0 votes)
64 views

String Functions: STR 'This Is A Test ' Whos

The document discusses string functions in MATLAB. It describes that a string is a character array that takes up two bytes per character. It then explains functions for converting between character and numeric arrays, concatenating strings, comparing strings for equality, searching and replacing characters within strings.

Uploaded by

chinu07416
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
64 views

String Functions: STR 'This Is A Test ' Whos

The document discusses string functions in MATLAB. It describes that a string is a character array that takes up two bytes per character. It then explains functions for converting between character and numeric arrays, concatenating strings, comparing strings for equality, searching and replacing characters within strings.

Uploaded by

chinu07416
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

String Functions

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

str = This is a test ;


Creates a 14-element character array. The output of whos for this array is >> whos str

Name str

Size 1x14

Bytes 28

Class char array

Grand Total is 14 elements using 28 bytes.


A special function ischar can be used to check for character arrays. If a given variable is of type character, then ischar returns a true (1) value. If it is not, ischar returns a false (0) value. The following subsections describe MATLAB functions useful for manipulating character strings.

String Conversion Functions


Variables may be converted from the char data type to the double data type using the double function. Thus the statement double (str) yields the result:

>> x = double (str)


x= Columns 1 through 12 84 104 105 115 32 105 115 32 97 32 116 101 Columns 13 through 14 115 116 Variables can also be converted from the double data type to the char data type using the char Function. If x is the 14- element array created above, then the statement char(x) yields the result:

>> z = char (x)


z=

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)

Compensating Strings for Equality


You can use four MATLAB functions to compare two strings as whole for equality. They are: strcmp determines if two strings are identical. strcmpi determines if two strings are identical ignoring case. strncmp determines if the first n characters of two strings are identical. strncmpi determines if the first n characters of two strings are identical ignoring case. strcmp This function compares two strings, including any leading and trailing blanks, and returns a true(1) if the strings are identical. Otherwise, it returns a false(0).

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

Searching and Replacing Characters within a String


findstr Function findstr returns the starting position of all occurrences of the shorter of two strings within a longer string. For example, to find all occurrences of the string is inside test, >>position = findstr(test,is) position = 3 6

The string is occurs twice within test, starting at positions 3 and 6

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!

>> result = strrep(test,test,pest) result = This is a pest!

You might also like