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

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

Post on 28-Dec-2015




1 download


<ul><li><p>StringsRepresentation and Manipulation</p></li><li><p>ObjectsObjects : Code entities uniting data and behaviorBuilt from primitive data types</p></li><li><p>Real World Objects</p><p>An object is tangibleAn object holds together as a single whole An object has properties An object can do things and can have things done to it</p><p>ObjectsNon-objectsA penThe upper 37% of the penA computer keyboardThe air above the keyboardA shoeThe color of the shoeA mouseThe sound of a mouse click</p></li><li><p>Code ObjectsModel objects &amp; conceptual entities</p><p>3 Key Things:state: it has various properties (data)behavior: things it can do things and that can be done to itidentity : each object is a distinct individual</p></li><li><p>StringsC++ strings Objects defined in library</p><p>Objects have:State : letters in the stringBehaviors : things we can do to/with string</p></li><li><p>StringsList of charactersIndexed by position starting from 0</p><p>string schoolName = "Chemeketa";</p><p>012345678Chemeketa</p></li><li><p>Behaviors. Operator : object.actionAsk object to do named action</p><p>string schoolName = "Chemeketa";cout </p></li><li><p>Accessing CharactersGet a[index]Unsafechar letter =; //letter = m</p><p>012345678Chemeketa</p></li><li><p>OperatorsAssignment changes stored valueAddition concatenates strings</p></li><li><p>Bad ConcatCan only concat strings to stringsBut, can add chars to a string variablechar + string literal = weirdness</p></li><li><p>ConversionsTurn number into string: to_string </p></li><li><p>ComparisonsRelational operators compare alphabeticallishCase mattersASCII based : lower case &gt; upper case</p></li><li><p> if strVar &gt; strVar2Zero if strVar == strVar2Negative if strVar &lt; strVar2</p></li><li><p>Inputcin &gt;&gt; string only gets one "word"</p></li><li><p>GetlineGetline retrieves everything up to newlinegetline(streamName, stringName)read from strreamNamestore into stringName</p></li><li><p>GetlineOptional 3rd parameter overrides delimitergetline(streamName, stringName, delimiter)read until we see delimiter not newline</p></li><li><p>LengthLength of stringstrVar.length()</p><p>int letterCount = schoolname.length();//letterCount = 9</p><p>012345678Chemeketa</p></li><li><p>Finding CharactersDoes something appear in string:strVar.find(str)</p><p>int location = schoolname.find("he");//location = 1 </p><p>012345678Chemeketa</p></li><li><p>Finding CharactersDoes something appear in string:strVar.find(str)-1 means not there ?!?!</p><p>int location = schoolname.find("bb");//location = -1 </p><p>012345678Chemeketa</p></li><li><p>SubstringsGet 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"</p><p>012345678Chemeketa</p></li><li><p>Modify StringsInsert characters into string:strVar.insert(pos, str)</p><p>schoolname.insert(1, "xx");//schoolname now "Cxxhemeketa</p><p>012345678Chemeketa</p></li><li><p>Modify StringsErase characters from string:strVar.erase(pos, number)strVar.erase(pos) //from pos to end</p><p>schoolname.erase(1, 2);//schoolname now "Cmeketa" schoolname.erase(5);//schoolname now "Cheme"</p><p>012345678Chemeketa</p></li><li><p>String Functions To KnowFunctions you should know:</p><p> the element at the position specified byindex.strVar[index]Returns the element at the position specified byindex.strVar.append(str) &gt; str;returns0ifstrVar == str;returns-1ifstrVar &lt; str.strVar.empty()ReturnstrueifstrVaris empty; otherwise, it returnsfalse.strVar.erase(pos)Deletes all the characters instrVar starting at posstrVar.erase(pos, n)Deletesncharacters fromstrVarstarting at positionpos.</p></li><li><p>String Functions To Know 2</p><p>strVar.find(str)Returns the index of the first occurrence ofstrinstrVar. Ifstris not found, the special valuestring::nposis returned.strVar.find(str, pos)Returns the index of the first occurrence at or afterposwherestris found instrVar.strVar.insert(pos, str)Inserts all the characters ofstrat indexposintostrVar.strVar.length()Returns a value of typestring::size_typegiving the number of charactersstrVar.strVar.replace(pos, n, str)Starting at indexpos, replaces the nextncharacters ofstrVarwith all the characters ofstr. Ifn </p></li><li><p>String Functions To Know 3</p><p>strVar.substr(pos)Returns a string that is a substring ofstrVarstarting atpos. All characters until the end of the string are returned.strVar.substr(pos, len)Returns a string that is a substring ofstrVarstarting atpos. The length of the substring is at mostlencharacters. Iflenis too large, it means to the end of the string instrVar.</p></li><li><p>Destructive!Most functions modify a stringsubstr returns a new string</p><p>If you want to keep original, make a copy:string copy = myString;</p></li><li><p>Java Comparison</p><p>C++JavaStrings are mutable (can change)Strings are immutable(can't change)Strings are copies on assignmentStrings 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.</p></li></ul>


View more >