Skip to main content

more options

String functions

A complete reference of all of MATLAB's string functions can be obtained by typing 'help strfun' at the MATLAB prompt.
Function/Syntax Description Example
t = [s1 s2 s3]
String concatenation
s1 = 'A dumb';  s2 = ' '; 
s3 = 'example';  
t = [s1 s2]  
⇒ t = 'A dumb example'
s(n)
s(n:m) 
String addressing/sectioning
 s = 'knowhow';
t1 = s(4)   ⇒ t1 = 'w'
t2 = s(4:6) ⇒ t2 = 'who' 
t = strcat(s1,s2,...) 
String concatenation s1, s2, s3 as above
t = strcat(s1,s2,s3)  
⇒ t = 'A dumb example'
t = char(x) 
Create string from numeric array.
t = char([77 65 84 76 65 66]) 
⇒ t = 'MATLAB'
t = num2str(x) 
Convert numbers to a string.
t = num2str(3.141516) 
⇒ t = '3.141516'
t = sprintf(FORMAT, A,B,...) 
Create a string by formating data arguments A,B,... according to the (c-language-like style) FORMAT string
t = sprintf('%0.5g',(1+sqrt(5))/2) 
⇒ t = '1.618'
t = lower(s)  
Convert string to lowercase.
 t =lower('Do not SHOUT!')
⇒ t = 'do not shout!'
t = upper(s)
Convert string to uppercase
 t = upper('I will say it louder')
⇒ t = 'I WILL SAY IT LOUDER'
yn = strcmp(s1,s2)  
returns logical 1 (true) if strings S1 and S2 are the same and logical 0 (false) otherwise
strcmp('not','Not') &rArr  0 
strcmp('the same', ['the ', 'same']) &rArr  1
 t = findstr(s1,s2) 
returns the starting indices of any occurrences of the shorter of the two strings in the longer.
 s = 'How much wood would a woodchuck chuck?';
t = findstr(s,'wood') ⇒  t= [10 23]
S = strvcat(s1,s2,s3,..) 
Vertically concatenate strings. Automatically pads each string with blanks in order to form a valid matrix.
S = strvcat('several','different', 'strs');
⇒ S = ['several  '
       'different'
       'strs     '];  
t = ischar(x) 
returns 1 if S is a character array and 0 otherwise.
ischar(123) ⇒ 0
ischar('123') ⇒ 1