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

Slides:



Advertisements
Similar presentations
 2003 Prentice Hall, Inc. All rights reserved Fundamentals of Characters and Strings Character constant –Integer value represented as character.
Advertisements

Strings.
Current Assignments Homework 5 will be available tomorrow and is due on Sunday. Arrays and Pointers Project 2 due tonight by midnight. Exam 2 on Monday.
Java Programming Strings Chapter 7.
Strings An extension of types A class that encompasses a character array and provides many useful behaviors Chapter 9 Strings are IMMUTABLE.
String and Lists Dr. Benito Mendoza. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list List.
©2004 Brooks/Cole Chapter 7 Strings and Characters.
Definition of Strings Generally speaking, a string is a sequence of characters c string c++ string class Examples: “hello”, “high school”, “H2O”. Typical.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Fundamentals of Strings and Characters Characters.
Chapter 9 Characters and Strings. Topics Character primitives Character Wrapper class More String Methods String Comparison String Buffer String Tokenizer.
1 Lecture 19:String Data Type Introduction to Computer Science Spring 2006.
Chapter 7. 2 Objectives You should be able to describe: The string Class Character Manipulation Methods Exception Handling Input Data Validation Namespaces.
Strings Edward J. Biebel. Strings Strings are fundamental part of all computing languages. At the basic level, they are just a data structure that can.
Section 2 - More Basics. The char Data Type Data type of a single character Example char letter; letter = 'C';
Elements of a C++ program 1. Review Algorithms describe how to solve a problem Structured English (pseudo-code) Programs form that can be translated into.
Chapter 8 Strings and Vectors (8.1 and 8.2). An Array of characters Defined as: char firstName[20]; char firstName[] = {‘T’, ‘i’, ‘m’}; // an array of.
CSIS 123A Lecture 6 Strings & Dynamic Memory. Introduction To The string Class Must include –Part of the std library You can declare an instance like.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 9 More About Strings.
String Class in Java java.lang Class String java.lang.Object java.lang.String java.lang.Object We do not have to import the String class since it comes.
Characters The data type char represents a single character in Java. –Character values are written as a symbol: ‘a’, ‘)’, ‘%’, ‘A’, etc. –A char value.
Strings The Basics. Strings can refer to a string variable as one variable or as many different components (characters) string values are delimited by.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 8: The string Type.
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
C++ PROGRAMMING: PROGRAM DESIGN INCLUDING DATA STRUCTURES, FIFTH EDITION Chapter 10: Strings and string type.
An Introduction to Java Programming and Object-Oriented Application Development Chapter 7 Characters, Strings, and Formatting.
Chapter 13 – C++ String Class. String objects u Do not need to specify size of string object –C++ keeps track of size of text –C++ expands memory region.
Chapter 7: Characters, Strings, and the StringBuilder.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 8: User-Defined Simple Data Types, Namespaces, and the string Type.
Characters, Strings, And The string Class Chapter 10.
Object Oriented Programming A new way of thinking.
Vladimir Misic: Characters and Strings1Tuesday, 9:39 AM Characters and Strings.
String String Builder. System.String string is the alias for System.String A string is an object of class string in the System namespace representing.
String Class. C-style and C++ string Classes C-style strings, called C-strings, consist of characters stored in an array ( we’ll look at them later) C++
C++ String Class nalhareqi©2012. string u The string is any sequence of characters u To use strings, you need to include the header u The string is one.
CSC 270 – Survey of Programming Languages
String Class Mohamed Shehata 1020: Introduction to Programming.
Chapter 3A Strings. Using Predefined Classes & Methods in a Program To use a method you must know: 1.Name of class containing method (Math) 2.Name of.
C++ for Engineers and Scientists Second Edition Chapter 7 Completing the Basics.
C++ STRINGS ● string is part of the Standard C++ Library ● new stuff: ● cin : standard input stream (normally the keyboard) of type istream. ● >> operator.
An Introduction to Programming with C++ Sixth Edition Chapter 13 Strings.
More about strings in C++. String member functions The next three slides present information about functions that are members of the C++ string class.
1 Predefined Classes and Objects Chapter 3. 2 Objectives You will be able to:  Use predefined classes available in the Java System Library in your own.
A FIRST BOOK OF C++ CHAPTER 14 THE STRING CLASS AND EXCEPTION HANDLING.
DCT1063 Programming 2 CHAPTER 3 STRINGS Mohd Nazri Bin Ibrahim Faculty of Computer, Media & Technology TATi University College
Strings, Characters, and Regular Expressions Session 10 Mata kuliah: M0874 – Programming II Tahun: 2010.
Strings, and the string Class. C-Strings C-string: sequence of characters stored in adjacent memory locations and terminated by NULL character The C-string.
String & Exception. Lesson plan Class & Object String Exercise for midterm.
Winter 2016CISC101 - Prof. McLeod1 CISC101 Reminders Quiz 3 this week – last section on Friday. Assignment 4 is posted. Data mining: –Designing functions.
String and Lists Dr. José M. Reyes Álamo. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list.
Strings.
String and Lists Dr. José M. Reyes Álamo.
Introduction Programs which manipulate character data don’t usually just deal with single characters, but instead with collections of them (e.g. words,
C-Strings We have already seen that a C-string is a null-terminated array of characters.
Computer Programming ||
Strings, StringBuilder, and Character
String String Builder.
Primitive Types Vs. Reference Types, Strings, Enumerations
Engineering 1020: Introduction to Programming Fall 2018
Chapter 7: Strings and Characters
MSIS 655 Advanced Business Applications Programming
Chapter 8: The string Type
10.1 Character Testing.
String class and its objects
String and Lists Dr. José M. Reyes Álamo.
Representation and Manipulation
Basic String Operations
Introduction to Computer Science
character manipulation
Presentation transcript:

Strings Representation and Manipulation

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

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

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

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 Chemeketa

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 newline getline(streamName, stringName) read from strreamName store into stringName

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

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

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

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

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

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

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

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

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.

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