1 / 26

Strings

Strings. Representation and Manipulation. Objects. Objects : Code entities uniting data and behavior Built from primitive data types. Real World Objects. An object is tangible An object holds together as a single whole An object has properties

abel-spence
Download Presentation

Strings

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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

  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

  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. Input • cin >> string only gets one "word"

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

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

  16. Length • Length of string • strVar.length() intletterCount = schoolname.length(); //letterCount = 9

  17. Finding Characters • Does something appear in string: • strVar.find(str) int location = schoolname.find("he"); //location = 1

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

  19. 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"

  20. Modify Strings • Insert characters into string: • strVar.insert(pos,str) schoolname.insert(1, "xx"); //schoolname now "Cxxhemeketa

  21. 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"

  22. String Functions To Know • Functions you should know:

  23. String Functions To Know 2

  24. String Functions To Know 3

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

  26. Java Comparison

More Related