Node:Searching and Replacing, Next:, Previous:Creating Strings, Up:Strings



Searching and Replacing

deblank (s) Function File
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.

findstr (s, t, overlap) Function File
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 ]

index (s, t) Function File
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.

rindex (s, t) Function File
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.

split (s, t) Function File
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"

strcmp (s1, s2) Function File
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.

strrep (s, x, y) Function File
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"

substr (s, beg, len) Function File
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)).