Compose-3000: Strings in OML
Tutorial Level: Beginner
- Create and assign a string.
- String concatenation.
- Replace a string.
- Split a string.
- Compare a string.
Creating and Assigning a String
Strings are any object enclosed by single quotes. For example:
'Hello World'
To assign this string to a variable we simply write:
a= 'Hello World'
Note that a double quote is not a valid string delimiter in this release of OML, and it will return an error:
Below are valid string literals:
Manipulating Strings with Concatenation
-
If you assign the string
'The quick brown fox jumps over the lazy dog'toaand a new stringbas'Hello World'you can’t add the two together. Doing so will result in an error:
Intuitively the expression
'a + b'should concatenate'b'onto the end of'a'. This, however, is not the case. In OML, strings are stored as vectors of ASCII characters (equivalent integers). As such, vectors with different length cannot be added. -
If you want to concatenate
'b'with'a', usestrcat(a,b)as follows:
-
You can also use a pair of brackets to do the concatenation.
[a,' ',b]is a joined stringaandb, with a white space in the middle, as shown below:
Replace a String
There are several ways to replace a substring with another:
-
You can use the built-in function strrep directly:
string = 'there are ten boys and ten girls.' strRep = strrep(string,'ten','five')
-
strrep replaces all substrings. If you only want to replace
the first
‘ten'in the previous example, you could use strfind to find the index of the first‘ten’, then do the concatenation. See the example below:strIn = 'there are ten boys and ten girls.' % find string first index = strfind(strIn,'ten'); index1 = index(1); index2 = index1 + length('ten'); strOut = strcat(strIn(1:index1-1),' five',strIn(index2:end))
-
You can also use the regexp function to find the first and
last index of the substring
'ten', then do the concatenation. See this example:strIn = 'there are ten boys and ten girls.' % using regexp index1 = regexp(strIn,'ten','start'); index2 = regexp(strIn,'ten','end'); strOut = strcat(strIn(1:index1(1)-1),'five',strIn(index2(1)+1:length(strIn)))This returns the same result as the previous example:
Split a String
-
To split the string into each word, use the built-in function
strsplit. You need to specify the separator for the
strings; the default separator is a blank space.
a = 'The quick brown fox jumps over the lazy dog' % split a string, default separator is blank space b = strsplit(a)
This results in a cell of strings with each word delineated by the white space between them.
-
Sometimes, separators in a string may not always be consistent. You can specify
all separators in a cell in this case, as shown in this example:
time = '2016/2/23,2016-2-24' % specify separators in a cell strOut = strsplit(time,{'/','-',','})
Compare Strings
-
To compare a string, use the strcmp function. It returns 1
(
true) if the two strings compared are identical, otherwise it returns 0 (false). See the example below:str1 = 'this is str1'; str2 = 'this is str2'; out1 = strcmp(str1,str2)This returns
falsesincestr1andstr2are not identical:
-
If you want to compare the first
nelements of two strings, use strncmp. In the previous example, the first 11 elements are identical, so it returnstrue:str1 = 'this is str1'; str2 = 'this is str2'; out1 = strncmp(str1,str2,11)Result:
-
If the differences in letter case aren’t a concern, you can use
strcmpi:
str3 = 'Abc'; str4 = 'ABC'; out3 = strcmp(str3,str4) % case insensitive out4 = strcmpi(str3,str4)Result: