270 likes | 415 Views
Strings. Representation and Manipulation. Objects. Objects : Code entities uniting data and behavior Built from primitive data types. Real World Objects. An object is tangible An object holds together as a single whole An object has properties
E N D
Strings Representation and Manipulation
Objects • Objects : Code entities uniting data and behavior • Built from primitive data types
Real World Objects • An object is tangible • An object holds together as a single whole • An object has properties • An object can do things and can have things done to it
Code Objects • Model objects & conceptual entities • 3 Key Things: • state: it has various properties (data) • behavior: things it can do things and that can be done to it • identity: each object is a distinct individual
Strings • C++ strings • Objects defined in <string> library • Objects have: • State : letters in the string • Behaviors : things we can do to/with string
Strings • List of characters • Indexed by position starting from 0 string schoolName = "Chemeketa";
Behaviors • . Operator : object.action • Ask object to do named action string schoolName = "Chemeketa"; cout << schoolName.at(2); //ask school name to give us character at location 2
Accessing Characters • Get a character • strVar.at(index) • Safe • strVar[index] • Unsafe char letter = schoolname.at(3); //letter = m
Operators • Assignment changes stored value • Addition concatenates strings
Bad Concat • Can only concat strings to strings • But, can add chars to a string variable • char + string literal = weirdness
Conversions • Turn number into string:to_string
Comparisons • Relational operators compare alphabeticallish • Case matters • ASCII based : lower case > upper case
Input • cin >> string only gets one "word"
Getline • Getline retrieves everything up to newline getline(streamName, stringName) • read from strreamName • store into stringName
Getline • Optional 3rd parameter overrides delimiter getline(streamName, stringName, delimiter) • read until we see delimiter not newline
Length • Length of string • strVar.length() intletterCount = schoolname.length(); //letterCount = 9
Finding Characters • Does something appear in string: • strVar.find(str) int location = schoolname.find("he"); //location = 1
Finding Characters • Does something appear in string: • strVar.find(str) • -1 means not there ?!?! int location = schoolname.find("bb"); //location = -1
Substrings • Get part of a string: • strVar.substr(pos,len) • strVar.substr(pos) //from pos to end string part = schoolname.substr(3, 2); string rest = schoolname.substr(5); //part = "me", rest = "keta"
Modify Strings • Insert characters into string: • strVar.insert(pos,str) schoolname.insert(1, "xx"); //schoolname now "Cxxhemeketa
Modify Strings • Erase characters from string: • strVar.erase(pos, number) • strVar.erase(pos) //from pos to end schoolname.erase(1, 2); //schoolname now "Cmeketa" schoolname.erase(5); //schoolname now "Cheme"
String Functions To Know • Functions you should know:
Destructive! • Most functions modify a string • substr returns a new string • If you want to keep original, make a copy: string copy = myString;