Upload
michael-gordon
View
387
Download
0
Embed Size (px)
DESCRIPTION
Citation preview
CS111 LabStrings, continued
Instructor: Michael Gordon
Substrings
Use .substr() to get a substring
First parameter is starting index, second
(optional) is the number of characters in
the substring. (By default it is all of them.)
string s = “Ronald”;
cout<<s.substr(0); //prints Ronald
cout<<s.substr(0,3); //prints Ron
cout<<s.substr(1,2); //prints on
Insert and find
string s = “AM”;
string s1 = s.insert(1,”DA”);
Inserts “DA” into s starting at position 1.
cout<<s1; //prints ADAM
s.find(“AD”); //value of first position
Comparison
Use ==, <, >, !=, <=, >=
Comparison is on ASCII value, so ‘A’ < ‘a’, (‘A’ == 65 and ‘a’ == 97), so comparisons are most useful on all lower-case or all upper-case strings.
A useful tool is the toupper(c) is a function that takes a char parameter and returns the uppercase version.
Cstring equivalents
#include <cstring>
Cstrings are declared as char cs[] = ….
strlen(cs); //returns the length of cs
strcat(cs1,cs2); //concatenates two
cstrings