390 likes | 585 Views
String Functions. Getting Numeric Equivalents of Chars. >> double('abc xyz') ans = 97 98 99 32 120 121 122 >> double('ABC XYZ') ans = 65 66 67 32 88 89 90. Converting Numbers Back to Strings. >> x = double('here is a text') x =
E N D
Getting Numeric Equivalents of Chars >> double('abc xyz') ans = 97 98 99 32 120 121 122 >> double('ABC XYZ') ans = 65 66 67 32 88 89 90
Converting Numbers Back to Strings >> x = double('here is a text') x = 104 101 114 101 32 105 115 32 97 32 116 101 120 116 >> xst = char(x) xst = here is a text
Converting to Upper Case • Given a string st, convert all the chars to upper case. (Matlab has built-in functions upper & lower) • Observation : the difference between the lower case and upper case of the same character is same for all characters. >> double('a') - double('A') ans = 32 >> double('f') - double('F') ans = 32
Algorithm Given st : stvector = numeric vector of st For all elements in stvector do if current char lower case change the element to upper case
isLower Function function result = isLower(ch) if (double(ch) >= double(‘a’) && double(ch) <= double(‘z’)) result = true; else result = false; end
toUpper Function function upperst = toUpper(st) diff = double('a') - double('A'); stvector = double(st); for i=1 : length(st) if isLower(st(i)) stvector(i) = stvector(i) - diff; end end upperst = char(stvector);
Converting string into integer function result = str2int( st ) result = 0; for i = 1:length(st) digitst = st(i); digit = double(digitst) - double('0'); % digitst - '0' also works result = 10 * result + digit; end
Getting parts of a string • In general : st(v), where v is a vector of locations that will be included. • Examples : >> st = 'abcdefgh‘; >> st( [1 3 6] ) ans = acf >> st(2:5) ans =bcde
Categorizing characters • ISLETTER True for letters of the alphabet. • ISSPACE True for white space characters. >> isletter('s') ans = 1 >> isletter('3') ans = 0 >> isspace(' ') ans = 1 >> isletter('a 3b 4e') ans = 1 0 0 1 0 0 1
deblank function • DEBLANK(S) removes trailing blanks from string S. >>st = ‘ abc abc ‘; >> ['---->' deblank(st) '<-----'] ans = ----> abc abc<-----
Getting Rid of Space Characters • Let’s implement a function, “trim”, similar to deblank that also removes leading spaces • Example: ‘ abc ‘ ‘abc’
Trim function algorithm • Find the location of first non-space character • Find the location of the last non-space character • Return the result as the subset in between these two locations
Trim function function result = trim( st ) first = 1; while (isspace(st(first))) first = first + 1; end last = length(st); while (isspace(st(last))) last = last - 1; end result = st(first:last);
Trim all spaces? • Let’s write a function that removes all space characters. • Example : ‘ ab cd e f’ ‘abcdef’ • As an intermediate step, let’s first list locations of all non-space characters
Listing locations of non-space characters for i = 1: length(st) if ~isspace(st(i)) disp([i]) end end • You can view this segment as generating numbers and processing them (here processing amounts to displaying on screen)
Crating a locations vector • If we put these locations inside a vector, rather than displaying, we could use this vector to get all non-space characters out of our string • Let’s first look at how we can create a new vector out of user input. We will keep putting elements until user enters –1.
Incrementally adding elements to a vector at command prompt >> v ??? Undefined function or variable 'v'. >> v(1) = 3; >> v v = 3 >> v(2) = 5 v = 3 5 >> v(3) = 7 v = 3 5 7
Creating a new vector of unknown size from user input function v = createVect nexti = 1; x = input('enter a number >'); while (x ~= -1) v(nexti) = x; nexti = nexti + 1; x = input('enter a number >'); end
Complete Program • Now we can merge the segment that generates the indices of non-space characters with the segment that creates a new vector to come up with our final solution
trimAll function function result = trimAll( st ) nexti = 1; for i = 1: length(st) if ~isspace(st(i)) nonspace(nexti) = i; nexti = nexti + 1; end end result = st(nonspace);
Comparing Strings • STRCMP determines if two strings are identical • STRCMPI Compare strings ignoring case. • STRNCMP Compare first N characters of strings. • STRNCMPI Compare first N characters of strings ignoring case.
Examples >> strcmp('abc' , 'aBc') ans = 0 >> strcmpi('abc' , 'aBc') ans = 1 >> strncmp('abcxyz' , 'aBcpqr', 3) ans = 0 >> strncmpi('abcxyz' , 'aBcpqr', 3) ans = 1
Implement strcmp function result = myStrCmp(st1, st2) if (length(st1) ~= length(st2)) result = false; else result = true; for i = 1 : length(st1) if (st1(i) ~= st2(i)) result = false; %break end end end function result = myStrCmp(st1, st2) if (length(st1) ~= length(st2)) result = false; return; end result = true; for i = 1 : length(st1) if (st1(i) ~= st2(i)) result = false; %break end end
Implement strcmp function result = myStrCmp(st1, st2) if (length(st1) ~= length(st2)) result = false; return; end i=1; while (i <= length(st1) && st1(i) == st2(i)) i = i + 1; end result = ~(i <= length(st1));
Implementation • Let’s implement strcmpi, strncmp, strncmpi ourselves using strcmp
Implement strcmpi function result = myStrCmpi(st1, st2) if (length(st1) ~= length(st2)) result = false; return; end diff = 'a' - 'A'; i=1; while ( (i <= length(st1)) && ... ( (st1(i) == st2(i)) || ... (isletter(st1(i)) && isletter(st2(i)) && abs(st1(i) - st2(i)) == diff ) ) ) i = i + 1; end result = ~(i <= length(st1));
… (first part omitted ) diff = 'a' - 'A'; i=1; equali = true; while (i <= length(st1) && equali) if (st1(i) == st2(i)) equali = true; elseif (isletter(st1(i)) && isletter(st2(i))) if (abs(st1(i) - st2(i)) == diff ) equali = true; else equali = false; end else equali = false; end i= i + 1; end result = ~(i <= length(st1));
strcmpi implementation … function result = myStrCmpi(st1, st2) if (length(st1) ~= length(st2)) result = false; return; end stu1 = upper(st1); stu2 = upper(st2); result = strcmp(stu1, stu2);
Strncmp implementation function result = myStrnCmp(st1, st2, n) i=1; while (i <= n && i <= length(st1) && i <= length(st2) && st1(i) == st2(i)) i = i + 1; end result = ~(i <= n);
Strncmp implementation function result = myStrnCmp(st1, st2, n) if (length(st1) < n || length(st2) < n) result = false; return end stn1 = st1(1 : n); stn2 = st2(1 : n); result = strcmp(stn1, stn2);
Findstr function FINDSTR Find one string within another. K = FINDSTR(S1,S2) returns the starting indices of any occurrences of the shorter of the two strings in the longer. FINDSTR is symmetric in its two arguments; that is, either S1 or S2 may be the shorter pattern to be searched for in the longer string. If you do not want this behavior, use STRFIND instead. Examples s = 'How much wood would a woodchuck chuck?'; findstr(s,'a') returns 21 findstr('a',s) returns 21 findstr(s,'wood') returns [10 23] findstr(s,'Wood') returns [] findstr(s,' ') returns [4 9 14 20 22 32]
Implement findstr … • Assume the first string is the longer one. • Use strcmp on subsets of the longer string
myFindStr function function result = myFindStr( st, search ) nexti = 1; for j = 1 : length(st) - length(search) + 1 if (strcmp(search, st(j : j + length(search) - 1))) result(nexti) = j; nexti = nexti + 1; end end
Strrep function STRREP Replace string with another. S = STRREP(S1,S2,S3) replaces all occurrences of the string S2 in string S1 with the string S3. The new string is returned. Example: s1='This is a good example'; strrep(s1,'good','great') returns 'This is a great example' strrep(s1,'bad','great') returns 'This is a good example' strrep(s1,'','great') returns 'This is a good example'