Strings Representation and Manipulation. Objects Objects : Code entities uniting data and behavior...

Preview:

Citation preview

StringsRepresentation 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

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";

0 1 2 3 4 5 6 7 8C h e m e k e t a

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

0 1 2 3 4 5 6 7 8C h e m e k e t a

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

Comparisons

• strVar.compare(strVar2)• Positive if strVar > strVar2

• Zero if strVar == strVar2

• Negative if strVar < strVar2

Input

• cin >> string only gets one "word"

Getline

• Getline retrieves everything up to newlinegetline(streamName, stringName)

• read from strreamName• store into stringName

Getline

• Optional 3rd parameter overrides delimitergetline(streamName, stringName, delimiter)

• read until we see delimiter not newline

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

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

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

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

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

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

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.

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.

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.

Destructive!

• Most functions modify a string– substr returns a new string

• If you want to keep original, make a copy:

string copy = myString;

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.