String Functions
1. Case Conversion: convstr()
convstr(str, [flag]) The convstr function converts the matrix of strings str into
lower case (for flag = "l"; default value) or upper case (for flag = "u").
Eg:
A=
['welcome','to' ,'strings']
convstr(A,'u')
output: !WELCOME TO STRINGS !
2. grep() : Find matches of a string in a vector of strings
Eg: 1. txt=['find matches of a string in a vector of strings'
'search position of a character string in an other string'
'Compare Strings'];
grep(txt,'strings')
ans :
1.
grep(txt,['strings' 'Strings'])
ans: 1.
1.
2. str = ["hat";"cat";"hhat";"chat";"hcat";"ccchat";"at";"dog"]
str =
!hat
!
!
!
!cat
!
!
!
!hhat !
!
!
!chat !
!
!
!hcat !
!
!
!ccchat !
!
!
!at
!
!
!
!dog
!
grep(str,'/[hc]+at/','r') -- Return a vector of indices for which the string
ends with at and also contain either h or c in the previous character
ans
1.
2.
3.
4.
5.
6.
grep(str,'/cat|dog/','r') --- Return a vector of indices for which the strings
contain cat or dog.
ans
= 2.
5.
8.
3. isalphanum ()
It checks if characters of a string are alphanumerics.
s = '%%%%WelcometoStrings!!!!';
isalphanum(s)
ans = F F F F T T T T T T T T T T T T T T T T F F F F
4. isascii(str) -Return Boolean if the given string is in 7 bit US-ASCII character code
letters = [115.
99.
105.
108.
105.
108.
97.
97.
98.]
letters =
115.
99.
98.
isascii(letters)
ans = T T T T T T
ascii(letters) --- returns the character for the respective codes.
ans = scilab
5. isdigit(Str) check that characters of a string are digits between 0 and 9 and returns a
Boolean matrix
6. isletter(str) --- check that characters of a string are alphabetic letters and returns a
Boolean matrix
7. isnum(str)
-- checks if a string represents a number and returns a Boolean matrix.
Eg: s = ['123','A','$'];
isnum(s)
ans = T F F
8. Length(str) Returns the length of the string
Eg: s = ['123','A','$'];
length(s)
ans =
3.
1.
letters = [115.
1.
99.
105.
108.
97.
98.]
length(letters)
ans =
6.
9. Strrev(Str) Reverses the given string
strrev('SCILab')
ans = baLICS
10. strchr It finds the first occurrence of a character in a string
11. strrchr It finds the last occurrence of a character in a string
s = 'hello everybody. Is everyone fine over there!!';
strchr(s,'o')
ans = o everybody. Is everyone fine over there!!
strrchr(s,'e')
ans = e!!
12.
strstr(str,substring) It helps in locating the substring from the
given string.
strstr(s,'fine')
ans =
fine over there!!
13.
Strindex(str,element) Return the index position of the element
present in the given entire string.
strindex(s,'e')
ans =
2.
7.
9.
22.
24.
29.
34.
38.
43.
45.
14. Strcmp(str1,str2) - Compares two strings str1 and str2. If both strings are same
0 is returned. If they are reverse, 1 is returned. If there is a mismatch, then -1 is
returned. Case dependent.
strcmp('Scilab','scilab')
ans = - 1.
strcmp('scilab','scilab')
ans =
0.
strcmp('scilab','balics')
ans =
1.
15. strcmpi(str1,str2) --- case independent.
strcmpi('Scilab','scilab')
ans =
0.