27
Strings Representation and Manipulation

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

Embed Size (px)

Citation preview

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

StringsRepresentation and Manipulation

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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.