strings representation and manipulation. objects objects : code entities uniting data and behavior...
TRANSCRIPT
![Page 1: Strings Representation and Manipulation. Objects Objects : Code entities uniting data and behavior – Built from primitive data types](https://reader036.vdocuments.mx/reader036/viewer/2022062407/56649e695503460f94b660eb/html5/thumbnails/1.jpg)
StringsRepresentation and Manipulation
![Page 2: Strings Representation and Manipulation. Objects Objects : Code entities uniting data and behavior – Built from primitive data types](https://reader036.vdocuments.mx/reader036/viewer/2022062407/56649e695503460f94b660eb/html5/thumbnails/2.jpg)
Objects
• Objects : Code entities uniting data and behavior– Built from primitive data types
![Page 3: Strings Representation and Manipulation. Objects Objects : Code entities uniting data and behavior – Built from primitive data types](https://reader036.vdocuments.mx/reader036/viewer/2022062407/56649e695503460f94b660eb/html5/thumbnails/3.jpg)
Real World Objects
Objects Non-objects A pen The upper 37% of the pen A computer keyboard The air above the keyboard A shoe The color of the shoe A mouse The sound of a mouse click
• 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
![Page 4: Strings Representation and Manipulation. Objects Objects : Code entities uniting data and behavior – Built from primitive data types](https://reader036.vdocuments.mx/reader036/viewer/2022062407/56649e695503460f94b660eb/html5/thumbnails/4.jpg)
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
![Page 5: Strings Representation and Manipulation. Objects Objects : Code entities uniting data and behavior – Built from primitive data types](https://reader036.vdocuments.mx/reader036/viewer/2022062407/56649e695503460f94b660eb/html5/thumbnails/5.jpg)
Strings
• C++ strings – Objects defined in <string> library
• Objects have:– State : letters in the string– Behaviors : things we can do to/with string
![Page 6: Strings Representation and Manipulation. Objects Objects : Code entities uniting data and behavior – Built from primitive data types](https://reader036.vdocuments.mx/reader036/viewer/2022062407/56649e695503460f94b660eb/html5/thumbnails/6.jpg)
Strings
• List of characters– Indexed by position starting from 0
string schoolName = "Chemeketa";
0 1 2 3 4 5 6 7 8C h e m e k e t a
![Page 7: Strings Representation and Manipulation. Objects Objects : Code entities uniting data and behavior – Built from primitive data types](https://reader036.vdocuments.mx/reader036/viewer/2022062407/56649e695503460f94b660eb/html5/thumbnails/7.jpg)
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
![Page 8: Strings Representation and Manipulation. Objects Objects : Code entities uniting data and behavior – Built from primitive data types](https://reader036.vdocuments.mx/reader036/viewer/2022062407/56649e695503460f94b660eb/html5/thumbnails/8.jpg)
Accessing Characters
• Get a character– strVar.at(index)• Safe
– strVar[index]• Unsafe
char letter = schoolname.at(3); //letter = m
0 1 2 3 4 5 6 7 8C h e m e k e t a
![Page 9: Strings Representation and Manipulation. Objects Objects : Code entities uniting data and behavior – Built from primitive data types](https://reader036.vdocuments.mx/reader036/viewer/2022062407/56649e695503460f94b660eb/html5/thumbnails/9.jpg)
Operators
• Assignment changes stored value• Addition concatenates strings
![Page 10: Strings Representation and Manipulation. Objects Objects : Code entities uniting data and behavior – Built from primitive data types](https://reader036.vdocuments.mx/reader036/viewer/2022062407/56649e695503460f94b660eb/html5/thumbnails/10.jpg)
Bad Concat
• Can only concat strings to strings– But, can add chars to a string variable– char + string literal = weirdness
![Page 11: Strings Representation and Manipulation. Objects Objects : Code entities uniting data and behavior – Built from primitive data types](https://reader036.vdocuments.mx/reader036/viewer/2022062407/56649e695503460f94b660eb/html5/thumbnails/11.jpg)
Conversions
• Turn number into string:to_string
![Page 12: Strings Representation and Manipulation. Objects Objects : Code entities uniting data and behavior – Built from primitive data types](https://reader036.vdocuments.mx/reader036/viewer/2022062407/56649e695503460f94b660eb/html5/thumbnails/12.jpg)
Comparisons
• Relational operators compare alphabeticallish– Case matters– ASCII based : lower case > upper case
![Page 13: Strings Representation and Manipulation. Objects Objects : Code entities uniting data and behavior – Built from primitive data types](https://reader036.vdocuments.mx/reader036/viewer/2022062407/56649e695503460f94b660eb/html5/thumbnails/13.jpg)
Comparisons
• strVar.compare(strVar2)• Positive if strVar > strVar2
• Zero if strVar == strVar2
• Negative if strVar < strVar2
![Page 14: Strings Representation and Manipulation. Objects Objects : Code entities uniting data and behavior – Built from primitive data types](https://reader036.vdocuments.mx/reader036/viewer/2022062407/56649e695503460f94b660eb/html5/thumbnails/14.jpg)
Input
• cin >> string only gets one "word"
![Page 15: Strings Representation and Manipulation. Objects Objects : Code entities uniting data and behavior – Built from primitive data types](https://reader036.vdocuments.mx/reader036/viewer/2022062407/56649e695503460f94b660eb/html5/thumbnails/15.jpg)
Getline
• Getline retrieves everything up to newlinegetline(streamName, stringName)
• read from strreamName• store into stringName
![Page 16: Strings Representation and Manipulation. Objects Objects : Code entities uniting data and behavior – Built from primitive data types](https://reader036.vdocuments.mx/reader036/viewer/2022062407/56649e695503460f94b660eb/html5/thumbnails/16.jpg)
Getline
• Optional 3rd parameter overrides delimitergetline(streamName, stringName, delimiter)
• read until we see delimiter not newline
![Page 17: Strings Representation and Manipulation. Objects Objects : Code entities uniting data and behavior – Built from primitive data types](https://reader036.vdocuments.mx/reader036/viewer/2022062407/56649e695503460f94b660eb/html5/thumbnails/17.jpg)
Length
• Length of string– strVar.length()
int letterCount = schoolname.length();
//letterCount = 9
0 1 2 3 4 5 6 7 8C h e m e k e t a
![Page 18: Strings Representation and Manipulation. Objects Objects : Code entities uniting data and behavior – Built from primitive data types](https://reader036.vdocuments.mx/reader036/viewer/2022062407/56649e695503460f94b660eb/html5/thumbnails/18.jpg)
Finding Characters
• Does something appear in string:– strVar.find(str)
int location = schoolname.find("he");
//location = 1
0 1 2 3 4 5 6 7 8C h e m e k e t a
![Page 19: Strings Representation and Manipulation. Objects Objects : Code entities uniting data and behavior – Built from primitive data types](https://reader036.vdocuments.mx/reader036/viewer/2022062407/56649e695503460f94b660eb/html5/thumbnails/19.jpg)
Finding Characters
• Does something appear in string:– strVar.find(str)– -1 means not there ?!?!
int location = schoolname.find("bb");
//location = -1
0 1 2 3 4 5 6 7 8C h e m e k e t a
![Page 20: Strings Representation and Manipulation. Objects Objects : Code entities uniting data and behavior – Built from primitive data types](https://reader036.vdocuments.mx/reader036/viewer/2022062407/56649e695503460f94b660eb/html5/thumbnails/20.jpg)
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"
0 1 2 3 4 5 6 7 8C h e m e k e t a
![Page 21: Strings Representation and Manipulation. Objects Objects : Code entities uniting data and behavior – Built from primitive data types](https://reader036.vdocuments.mx/reader036/viewer/2022062407/56649e695503460f94b660eb/html5/thumbnails/21.jpg)
Modify Strings
• Insert characters into string:– strVar.insert(pos, str)
schoolname.insert(1, "xx");
//schoolname now "Cxxhemeketa
0 1 2 3 4 5 6 7 8C h e m e k e t a
![Page 22: Strings Representation and Manipulation. Objects Objects : Code entities uniting data and behavior – Built from primitive data types](https://reader036.vdocuments.mx/reader036/viewer/2022062407/56649e695503460f94b660eb/html5/thumbnails/22.jpg)
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"
0 1 2 3 4 5 6 7 8C h e m e k e t a
![Page 23: Strings Representation and Manipulation. Objects Objects : Code entities uniting data and behavior – Built from primitive data types](https://reader036.vdocuments.mx/reader036/viewer/2022062407/56649e695503460f94b660eb/html5/thumbnails/23.jpg)
String Functions To Know
• Functions you should know:strVar.at(index) Returns the element at the position specified
by index.
strVar[index] Returns the element at the position specified by index.
strVar.append(str) Appends str to strVar.
strVar.compare(str) Returns 1 if strVar > str; returns 0 if strVar == str; returns -1 ifstrVar < str.
strVar.empty() Returns true if strVar is empty; otherwise, it returns false.
strVar.erase(pos) Deletes all the characters in strVar starting at pos
strVar.erase(pos, n) Deletes n characters from strVar starting at position pos.
![Page 24: Strings Representation and Manipulation. Objects Objects : Code entities uniting data and behavior – Built from primitive data types](https://reader036.vdocuments.mx/reader036/viewer/2022062407/56649e695503460f94b660eb/html5/thumbnails/24.jpg)
String Functions To Know 2strVar.find(str) Returns the index of the first occurrence
of str in strVar. If str is not found, the special value string::npos is returned.
strVar.find(str, pos) Returns the index of the first occurrence at or after pos where str is found in strVar.
strVar.insert(pos, str) Inserts all the characters of str at index pos into strVar.
strVar.length() Returns a value of type string::size_type giving the number of characters strVar.
strVar.replace(pos, n, str) Starting at index pos, replaces the next n characters of strVar with all the characters of str. If n < length of strVar, then all the characters until the end of strVar are replaced.
![Page 25: Strings Representation and Manipulation. Objects Objects : Code entities uniting data and behavior – Built from primitive data types](https://reader036.vdocuments.mx/reader036/viewer/2022062407/56649e695503460f94b660eb/html5/thumbnails/25.jpg)
String Functions To Know 3strVar.substr(pos) Returns a string that is a substring of strVar starting
at pos. All characters until the end of the string are returned.
strVar.substr(pos, len) Returns a string that is a substring of strVar starting at pos. The length of the substring is at most len characters. If len is too large, it means “to the end” of the string in strVar.
![Page 26: Strings Representation and Manipulation. Objects Objects : Code entities uniting data and behavior – Built from primitive data types](https://reader036.vdocuments.mx/reader036/viewer/2022062407/56649e695503460f94b660eb/html5/thumbnails/26.jpg)
Destructive!
• Most functions modify a string– substr returns a new string
• If you want to keep original, make a copy:
string copy = myString;
![Page 27: Strings Representation and Manipulation. Objects Objects : Code entities uniting data and behavior – Built from primitive data types](https://reader036.vdocuments.mx/reader036/viewer/2022062407/56649e695503460f94b660eb/html5/thumbnails/27.jpg)
Java Comparison
C++ JavaStrings are mutable (can change)
Strings are immutable(can't change)
Strings are copies on assignment
Strings are not copied on assignment (but they are immutable)
Feel free to do dumb stuff. I may stop you. Or not.
I will throw a nice clean exception if you go out of the string bounds.