Presentation is loading. Please wait.

Presentation is loading. Please wait.

Strings Representation and Manipulation. Objects Objects : Code entities uniting data and behavior – Built from primitive data types.

Similar presentations


Presentation on theme: "Strings Representation and Manipulation. Objects Objects : Code entities uniting data and behavior – Built from primitive data types."— Presentation transcript:

1 Strings Representation and Manipulation

2 Objects Objects : Code entities uniting data and behavior – Built from primitive data types

3 Real World Objects ObjectsNon-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

4 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

5 Strings C++ strings – Objects defined in library Objects have: – State : letters in the string – Behaviors : things we can do to/with string

6 Strings List of characters – Indexed by position starting from 0 string schoolName = "Chemeketa"; 012345678 Chemeketa

7 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

8 Accessing Characters Get a character – strVar.at(index) Safe – strVar[index] Unsafe char letter = schoolname.at(3); //letter = m 012345678 Chemeketa

9 Operators Assignment changes stored value Addition concatenates strings

10 Bad Concat Can only concat strings to strings – But, can add chars to a string variable – char + string literal = weirdness

11 Conversions Turn number into string: to_string

12 Comparisons Relational operators compare alphabeticallish – Case matters – ASCII based : lower case > upper case

13 Comparisons strVar.compare(strVar2) Positive if strVar > strVar2 Zero if strVar == strVar2 Negative if strVar < strVar2

14 Input cin >> string only gets one "word"

15 Getline Getline retrieves everything up to newline getline(streamName, stringName) read from strreamName store into stringName

16 Getline Optional 3 rd parameter overrides delimiter getline(streamName, stringName, delimiter) read until we see delimiter not newline

17 Length Length of string – strVar.length() int letterCount = schoolname.length(); //letterCount = 9 012345678 Chemeketa

18 Finding Characters Does something appear in string: – strVar.find(str) int location = schoolname.find("he"); //location = 1 012345678 Chemeketa

19 Finding Characters Does something appear in string: – strVar.find(str) – -1 means not there ?!?! int location = schoolname.find("bb"); //location = -1 012345678 Chemeketa

20 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" 012345678 Chemeketa

21 Modify Strings Insert characters into string: – strVar.insert(pos, str) schoolname.insert(1, "xx"); //schoolname now "Cxxhemeketa 012345678 Chemeketa

22 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" 012345678 Chemeketa

23 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.

24 String Functions To Know 2 strVar.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.

25 String Functions To Know 3 strVar.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.

26 Destructive! Most functions modify a string – substr returns a new string If you want to keep original, make a copy: string copy = myString;

27 Java Comparison C++Java Strings 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.


Download ppt "Strings Representation and Manipulation. Objects Objects : Code entities uniting data and behavior – Built from primitive data types."

Similar presentations


Ads by Google