Next: , Previous: Creating Strings, Up: Strings


5.2 Searching and Replacing

— Function File: deblank (s)

Removes the trailing blanks and nulls from the string s. If s is a matrix, deblank trims each row to the length of longest string.

— Function File: findstr (s, t, overlap)

Return the vector of all positions in the longer of the two strings s and t where an occurrence of the shorter of the two starts. If the optional argument overlap is nonzero, the returned vector can include overlapping positions (this is the default). For example,

          findstr ("ababab", "a")
          => [ 1, 3, 5 ]
          findstr ("abababa", "aba", 0)
          => [ 1, 5 ]
     

— Function File: index (s, t)

Return the position of the first occurrence of the string t in the string s, or 0 if no occurrence is found. For example,

          index ("Teststring", "t")
          => 4
     

Caution: This function does not work for arrays of strings.

— Function File: rindex (s, t)

Return the position of the last occurrence of the string t in the string s, or 0 if no occurrence is found. For example,

          rindex ("Teststring", "t")
          => 6
     

Caution: This function does not work for arrays of strings.

— Function File: split (s, t)

Divides the string s into pieces separated by t, returning the result in a string array (padded with blanks to form a valid matrix). For example,

          split ("Test string", "t")
          => "Tes "
                  " s  "
                  "ring"
     

— Function File: strcmp (s1, s2)

Compares two character strings, returning true if they are the same, and false otherwise.

Caution: For compatibility with Matlab, Octave's strcmp function returns true if the character strings are equal, and false otherwise. This is just the opposite of the corresponding C library function.

— Function File: strrep (s, x, y)

Replaces all occurrences of the substring x of the string s with the string y. For example,

          strrep ("This is a test string", "is", "&%$")
          => "Th&%$ &%$ a test string"
     

— Function File: substr (s, beg, len)

Return the substring of s which starts at character number beg and is len characters long.

If OFFSET is negative, extraction starts that far from the end of the string. If LEN is omitted, the substring extends to the end of S.

For example,

          substr ("This is a test string", 6, 9)
          => "is a test"
     
This function is patterned after AWK. You can get the same result by s (beg : (beg + len - 1)).