String Functions

base64decode

Decode base64-encoded data.

Syntax

strb = base64decode(strt)

Description

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.

See also

base64encode

base64encode

Encode data using base64.

Syntax

strt = base64encode(strb)

Description

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.

Example

s = base64encode(char(0:10))
  s =
    AAECAwQFBgcICQo=
double(base64decode(s))
  0  1  2  3  4  5  6  7  8  9 10

See also

base64decode

char

Convert an array to a character array (string).

Syntax

s = char(A)
S = char(s1, s2, ...)

Description

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.

Examples

char(65:70)
  ABCDEF
char([65, 66; 67, 68](:).')
  ABCD
char('ab','cde')
  ab
  cde
char('abc',['de';'fg'])
  abc
  de
  fg

See also

setstr, uint16, operator :, operator .', ischar, logical, double, single

deblank

Remove trailing blank characters from a string.

Syntax

s2 = deblank(s1)

Description

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).

Example

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

See also

strtrim

findstr

Find a substring in a string.

Syntax

pos = findstr(str, sub)

Description

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.

Examples

findstr('ababcdbaaab','ab')
  1 3 10
findstr('ababcdbaaab','ac')
  []
findstr('aaaaaa','aaa')
  1 2 3

See also

find, strcmp, strmatch, strtok

ischar

Test for a string object.

Syntax

b = ischar(obj)

Description

ischar(obj) is true if the object obj is a character string, false otherwise. Strings can have more than one line.

Examples

ischar('abc')
  true
ischar(0)
  false
ischar([])
  false
ischar('')
  true
ischar(['abc';'def'])
  true

See also

isletter, isspace, isnumeric, islogical, isinteger, islist, isstruct, setstr, char

isdigit

Test for digits.

Syntax

b = isdigit(s)

Description

For each character of string s, isdigit(s) is true if it is a digit ('0' to '9') and false otherwise.

Examples

isdigit('a123bAB12* ')
  F T T T F F F T T F F

See also

isletter, isspace, lower, upper, ischar

isletter

Test for letters.

Syntax

b = isletter(s)

Description

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.

Examples

isletter('abAB12* ')
  T T T T F F F F

See also

isdigit, isspace, lower, upper, ischar

isspace

Test for spaces.

Syntax

b = isspace(s)

Description

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.

Example

isspace('a\tb c\nd')
  0 1 0 1 0 1 0

See also

isletter, isdigit, ischar

lower

Convert all uppercase letters to lowercase.

Syntax

s2 = lower(s1)

Description

lower(s1) converts all the uppercase letters of string s1 to lowercase. Currently, only ASCII letters (without diacritic) are converted.

Example

lower('abcABC123')
  abcabc123

See also

upper, isletter

md5

Calculate MD5 digest.

Syntax

digest = md5(strb)
digest = md5(fd)

Description

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.

Examples

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);

See also

sha1

setstr

Conversion of an array to a string.

Syntax

str = setstr(A)

Description

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.

Example

setstr(65:75)
  ABCDEFGHIJK

See also

char, uint16, logical, double

sha1

Calculate SHA1 digest.

Syntax

digest = sha1(strb)
digest = sha1(fd)

Description

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.

Example

SHA1 digest of the three characters 'a', 'b', and 'c':

sha1('abc')
  a9993e364706816aba3e25717850c26c9cd0d89d

See also

md5

strcmp

String comparison.

Syntax

b = strcmp(s1, s2)
b = strcmp(s1, s2, n)

Description

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.

Examples

strcmp('abc','abc')
  true
strcmp('abc','def')
  false
strcmp('abc','abd',2)
  true
strcmp('abc','abc',5)
  false

See also

strcmpi, operator ===, operator ~==, operator ==, findstr, strmatch

strcmpi

String comparison with ignoring letter case.

Syntax

b = strcmpi(s1, s2)
b = strcmpi(s1, s2, n)

Description

strcmpi compares strings for equality, ignoring letter case. In every other respect, it behaves like strcmp.

Examples

strcmpi('abc','aBc')
  true
strcmpi('Abc','abd',2)
  true

See also

strcmp, operator ===, operator ~==, operator ==, findstr, strmatch

strmatch

String match.

Syntax

i = strmatch(str, strMatrix)
i = strmatch(str, strList)
i = strmatch(..., 'exact')

Description

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.

Examples

strmatch('abc',['axyz';'uabc';'abcd';'efgh'])
  3
strmatch('abc',['axyz';'uabc';'abcd';'efgh'],'exact')
  0
strmatch('abc',{'ABC','axyz','abcdefg','ab','abcd'})
  3

See also

strcmp, findstr

strtok

Token search in string.

Syntax

(token, remainder) = strtok(str)
(token, remainder) = strtok(str, separators)

Description

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.

Examples

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'

See also

strmatch, findstr, strtrim

strtrim

Remove leading and trailing blank characters from a string.

Syntax

s2 = strtrim(s1)

Description

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).

Example

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

See also

deblank, strtok

upper

Convert all lowercase letters to lowercase.

Syntax

s2 = upper(s1)

Description

upper(s1) converts all the lowercase letters of string s1 to uppercase. Currently, only ASCII letters (without diacritic) are converted.

Example

upper('abcABC123')
  ABCABC123

See also

lower, isletter

utf8decode

Decode Unicode characters encoded with UTF-8.

Syntax

str = utf8decode(b)

Description

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.

See also

utf8encode

utf8encode

Encode a string of Unicode characters using UTF-8.

Syntax

b = utf8encode(str)

Description

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.

Example

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

See also

utf8decode


Copyright 1998-2007, Calerga.
All rights reserved.