Decode base64-encoded data.
strb = base64decode(strt)
base64decode(strt) decodes the contents of string strt which represents data encoded with base64. Characters which are not 'A'-'Z', 'a'-'z', '0'-'9', '+', '/', or '=' are ignored. Decoding stops at the end of the string or when '=' is reached.
Encode data using base64.
strt = base64encode(strb)
base64encode(strb) encodes the contents of string strb which represents binary data. The result contains only characters 'A'-'Z', 'a'-'z', '0'-'9', '+', '/', and '='; it is suitable for transmission or storage on media which accept only text.
Each character of encoded data represents 6 bits of binary data; i.e. one needs four characters for three bytes. The six bits represent 64 different values, encoded with the characters 'A' to 'Z', 'a' to 'z', '0' to '9', '+', and '/' in this order. When the binary data have a length which is not a multiple of 3, encoded data are padded with one or two characters '=' to have a multiple of 4.
Base64 encoding is an Internet standard described in RFC 1521.
s = base64encode(char(0:10)) s = AAECAwQFBgcICQo= double(base64decode(s)) 0 1 2 3 4 5 6 7 8 9 10
Convert an array to a character array (string).
s = char(A) S = char(s1, s2, ...)
char(A) converts the elements of matrix A to characters, resulting in a string of the same size. Characters are stored in unsigned 16-bit words. The shape of A is preserved. Even if most functions ignore the string shape, you can force a row vector with char(A(:).').
char(s1,s2,...) concatenates vertically the arrays given as arguments to produce a string matrix. If the strings do not have the same number of columns, blanks are added to the right.
char(65:70) ABCDEF char([65, 66; 67, 68](:).') ABCD char('ab','cde') ab cde char('abc',['de';'fg']) abc de fg
setstr, uint16, operator :, operator .', ischar, logical, double, single
Remove trailing blank characters from a string.
s2 = deblank(s1)
deblank(s1) removes the trailing blank characters from string s1. Blank characters are spaces (code 32), tabulators (code 9), carriage returns (code 13), line feeds (code 10), and null characters (code 0).
double(' \tAB CD\r\n\0') 32 9 65 66 32 32 67 68 13 10 0 double(deblank(' \tAB CD\n\r\0'))) 32 9 65 66 32 32 67 68
Find a substring in a string.
pos = findstr(str, sub)
findstr(str,sub) finds occurrences of string sub in string str and returns a vector of the positions of all occurrences, or the empty vector [] if there is none. Occurrences may overlap.
findstr('ababcdbaaab','ab') 1 3 10 findstr('ababcdbaaab','ac') [] findstr('aaaaaa','aaa') 1 2 3
find, strcmp, strmatch, strtok
Test for a string object.
b = ischar(obj)
ischar(obj) is true if the object obj is a character string, false otherwise. Strings can have more than one line.
ischar('abc') true ischar(0) false ischar([]) false ischar('') true ischar(['abc';'def']) true
isletter, isspace, isnumeric, islogical, isinteger, islist, isstruct, setstr, char
Test for digits.
b = isdigit(s)
For each character of string s, isdigit(s) is true if it is a digit ('0' to '9') and false otherwise.
isdigit('a123bAB12* ') F T T T F F F T T F F
isletter, isspace, lower, upper, ischar
Test for letters.
b = isletter(s)
For each character of string s, isletter(s) is true if it is a letter and false otherwise. Letters with diacritical signs are not considered as letters.
isletter('abAB12* ') T T T T F F F F
isdigit, isspace, lower, upper, ischar
Test for spaces.
b = isspace(s)
For each character of string s, isspace(s) is true if it is a space, a tabulator, a carriage return or a line feed, and false otherwise.
isspace('a\tb c\nd') 0 1 0 1 0 1 0
Convert all uppercase letters to lowercase.
s2 = lower(s1)
lower(s1) converts all the uppercase letters of string s1 to lowercase. Currently, only ASCII letters (without diacritic) are converted.
lower('abcABC123') abcabc123
Calculate MD5 digest.
digest = md5(strb) digest = md5(fd)
md5(strb) calculates the MD5 digest of strb which represents binary data. strb can be a string (only the least-significant byte of each character is considered) or an array of bytes of class uint8 or int8. The result is a string of 32 hexadecimal digits. It is believed to be hard to create the input to get a given digest, or to create two inputs with the same digest.
md5(fd) calculates the MD5 digest of the bytes read from file descriptor fd until the end of the file. The file is left open.
MD5 digest is an Internet standard described in RFC 1321.
MD5 of the three characters 'a', 'b', and 'c':
md5('abc') 900150983cd24fb0d6963f7d28e17f72
This can be compared to the result of the command tool md5 found on many unix systems:
$ echo -n abc | md5 900150983cd24fb0d6963f7d28e17f72
The following statements calculate the digest of the file 'somefile':
fd = fopen('somefile'); digest = md5(fd); fclose(fd);
Conversion of an array to a string.
str = setstr(A)
setstr(A) converts the elements of array A to characters, resulting in a string of the same size. Characters are stored in unsigned 16-bit words.
setstr(65:75) ABCDEFGHIJK
Calculate SHA1 digest.
digest = sha1(strb) digest = sha1(fd)
sha1(strb) calculates the SHA1 digest of strb which represents binary data. strb can be a string (only the least-significant byte of each character is considered) or an array of bytes of class uint8 or int8. The result is a string of 40 hexadecimal digits. It is believed to be hard to create the input to get a given digest, or to create two inputs with the same digest.
sha1(fd) calculates the SHA1 digest of the bytes read from file descriptor fd until the end of the file. The file is left open.
SHA1 digest is an Internet standard described in RFC 3174.
SHA1 digest of the three characters 'a', 'b', and 'c':
sha1('abc') a9993e364706816aba3e25717850c26c9cd0d89d
String comparison.
b = strcmp(s1, s2) b = strcmp(s1, s2, n)
strcmp(s1, s2) is true if the strings s1 and s2 are equal (i.e. same length and corresponding characters are equal). strcmp(s1, s2, n) compares the strings up to the n:th character. Note that this function does not return the same result as the strcmp function of the standard C library.
strcmp('abc','abc') true strcmp('abc','def') false strcmp('abc','abd',2) true strcmp('abc','abc',5) false
strcmpi, operator ===, operator ~==, operator ==, findstr, strmatch
String comparison with ignoring letter case.
b = strcmpi(s1, s2) b = strcmpi(s1, s2, n)
strcmpi compares strings for equality, ignoring letter case. In every other respect, it behaves like strcmp.
strcmpi('abc','aBc') true strcmpi('Abc','abd',2) true
strcmp, operator ===, operator ~==, operator ==, findstr, strmatch
String match.
i = strmatch(str, strMatrix) i = strmatch(str, strList) i = strmatch(..., 'exact')
strmatch(str,strMatrix) compares string str with each row of the character matrix strMatrix; it returns the index of the first row whose beginning is equal to str, or 0 if no match is found. Case is significant.
strmatch(str,strList) compares string str with each element of list strList, which must be strings.
With a third argument, which must be the string 'exact', str must match the complete row or element of the second argument, not only the beginning.
strmatch('abc',['axyz';'uabc';'abcd';'efgh']) 3 strmatch('abc',['axyz';'uabc';'abcd';'efgh'],'exact') 0 strmatch('abc',{'ABC','axyz','abcdefg','ab','abcd'}) 3
Token search in string.
(token, remainder) = strtok(str) (token, remainder) = strtok(str, separators)
strtok(str) gives the first token in string str. A token is defined as a substring delimited by separators or by the beginning or end of the string; by default, separators are spaces, tabulators, carriage returns and line feeds. If no token is found (i.e. if str is empty or contains only separator characters), the result is the empty string.
The optional second output is set to what follows immediately the token, including separators. If no token is found, it is the same as str.
An optional second input argument contains the separators in a string.
Strings are displayed with quotes to show clearly the separators.
strtok(' ab cde ') 'ab' (t, r) = strtok(' ab cde ') t = 'ab' r = ' cde ' (t, r) = strtok('2, 5, 3') t = '2' r = ', 5, 3'
Remove leading and trailing blank characters from a string.
s2 = strtrim(s1)
strtrim(s1) removes the leading and trailing blank characters from string s1. Blank characters are spaces (code 32), tabulators (code 9), carriage returns (code 13), line feeds (code 10), and null characters (code 0).
double(' \tAB CD\r\n\0') 32 9 65 66 32 32 67 68 13 10 0 double(strtrim(' \tAB CD\n\r\0'))) 65 66 32 32 67 68
Convert all lowercase letters to lowercase.
s2 = upper(s1)
upper(s1) converts all the lowercase letters of string s1 to uppercase. Currently, only ASCII letters (without diacritic) are converted.
upper('abcABC123') ABCABC123
Decode Unicode characters encoded with UTF-8.
str = utf8decode(b)
utf8decode(b) decodes the contents of uint8 or int8 array b which represents Unicode characters encoded with UTF-8. Each Unicode character corresponds to one, two, or three bytes of UTF-8 code. The result is a standard character array with a single row. Invalid codes (for example when the beginning of the decoded data does not correspond to a character boundary) are ignored.
Encode a string of Unicode characters using UTF-8.
b = utf8encode(str)
utf8encode(b) encodes the contents of character array str using UTF-8. Each Unicode character in str corresponds to one, two, or three bytes of UTF-8 code. The result is an array of unsigned 8-bit integers.
If the input string does not contain Unicode characters, the output is invalid.
b = utf8encode(['abc', 200, 2000, 20000]) b = 1x10 uint8 array 97 98 99 195 136 223 144 228 184 160 str = utf8decode(b); +str 1x6 uint16 array 97 98 99 200 2000 20000