STL string class Programming Team 2/15/2006. string constructors string() = empty string string(int l, char ch) = string of length l, of repeated ch string(char*

Slides:



Advertisements
Similar presentations
Chapter 6 Lists and Dictionaries CSC1310 Fall 2009.
Advertisements

Ch 8. Characters and Strings Timothy Budd 2 Characters and Literals Strings Char in C++ is normally an 8-bit quantity, whereas in Java it is a 16-bit.
Classes and Data Abstraction Lecture 9. Objects Models of things in the real world Defined in classes  Class name is the object name Example: Library.
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.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12: More on C-Strings and the string Class Starting Out with.
Lecture 24: Strings. 2 Lecture Contents: t Library functions t Assignment and substrings t Concatenation t Comparison t Demo programs t Exercises.
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.
Beginning C++ Through Game Programming, Second Edition by Michael Dawson.
Definition of Strings Generally speaking, a string is a sequence of characters c string c++ string class Examples: “hello”, “high school”, “H2O”. Typical.
String class  Construct a string  String str = new String(“welcome”);  Char[] charr = {‘G’, ‘o’, ‘o’, ‘d’};  String mes = new String(charr);  A full.
CS31: Introduction to Computer Science I Discussion 1A 5/7/2010 Sungwon Yang
The Standard String Class Is actually a template: –typedef basic_string string This means you can have strings of things other than chars.
 2006 Pearson Education, Inc. All rights reserved Class string and String Stream Processing.
CSM-Java Programming-I Spring,2005 String Handling Lesson - 6.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 12 More.
C++ standard strings. Standard library of C++ language STL STL (The main part of standard library of C++ language) Stream classes Stream classes String.
CS 117 Spring 2002 Review for Exam 3 arrays strings files classes.
Operator Overloading CS 308 – Data Structures What is operator overloading? Changing the definition of an operator so it can be applied on the objects.
C-strings and C++ string Class
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
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.
String Constructors string str; // creates an empty //string string str(“abc”); // creates a string // from a C-string string str(aString); // creates.
Data Type string #include // C++ String class string str1, str2; // Default constructor cin >> str1 >> str2; cout
C++ String Class. Outline  String Initialization  Basic Operations  Comparisons  Substrings  Swapping Strings  String Size  Finding Strings and.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12: More on C-Strings and the string Class Starting Out with.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 26: Exam 2 Preview.
C++ PROGRAMMING: PROGRAM DESIGN INCLUDING DATA STRUCTURES, FIFTH EDITION Chapter 10: Strings and string type.
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.
String Handling StringBuffer class character class StringTokenizer class.
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
SMIE-121 Software Design II School of Mobile Information Engineering, Sun Yat-sen University Lecture.
CSI 3125, Preliminaries, page 1 String. CSI 3125, Preliminaries, page 2 String Class Java provides the String class to create and manipulate strings.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 10 Characters, Strings, and the string class.
CS 1430: Programming in C++ 1. Data Type string #include // C++ String class string str1, str2; // Default constructor cin >> str1 >> str2; cout
Chapter 6 LISTS AND STRINGS 1. List Definition 2. List Implementations (a) Class Templates (b) Contiguous (c) Simply Linked (d) Doubly Linked 3. Linked.
Chapter 16 C++ Strings By C. Shing ITEC Dept Radford University.
C++ Programming Basics
C++ STRINGS ● string is part of the Standard C++ Library ● new stuff: ● cin : standard input stream (normally the keyboard) of type istream. ● >> operator.
Department of Computer Science and Engineering, HKUST 1 HKUST Summer Programming Course 2008 Arrays, Character Strings and Standard Type String ~ Collection.
An Array Type For Strings. Two ways to represent strings – i.e. “Hello” cstring An array with base type char Older way of processing strings Null character.
Data Structure Specification  Language independent  Abstract Data Type  C++  Abstract Class.
C++ How to Program, 9/e © by Pearson Education, Inc. All Rights Reserved.
Strings Jarret Raim C Strings Same as arrays of characters. Use pointers. Require static declarations (compile time). No bounds checking. No easy.
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.
13. string Operations Intro Programming in C++ Computer Science Dept Va Tech August, 2002 © Barnette ND & McQuain WD 1 Accessing String Elements.
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.
The String class is defined in the java.lang package
C-Strings We have already seen that a C-string is a null-terminated array of characters.
Class string and String Stream Processing: A Deeper Look
EKT 472: Object Oriented Programming
String and String Buffers
String String Builder.
Object Oriented Programming
C++ STRINGS string is part of the Standard C++ Library
Summary of what we learned so far
Programming with ANSI C ++
Chapter 9 One-Dimensional Arrays
Announcements 3rd homework is due this week Wednesday (March 15)
String class and its objects
Java – String Handling.
Class string and String Stream Processing
Strings in Java.
Object Oriented Programming
An Introduction to Programming though C++
In Java, strings are objects that belong to class java.lang.String .
Chapter 12: More on C-Strings and the string Class
Presentation transcript:

STL string class Programming Team 2/15/2006

string constructors string() = empty string string(int l, char ch) = string of length l, of repeated ch string(char* c-string) = string from c-string string(char* c-string, int l) = string from c- string, starts at 0, goes l characters long (substring basically) string(string oldString, int start = 0, int length = npos) = copy constructor, allows substring

string operators By default, all comparison operators work ==,, >= + operator is concatenation 4 versions: string + string string + c-string c-string + string string + char

string constants.npos => length of longest possible string, usually -1 Useful when doing find operations (is this substring in the string?) They will usually return npos if not in there

string methods c_str() Returns a pointer to a c-style string copy(char* array, int length, int index=0) Beginning at index, copy length number of chars from string into char array

string methods empty() Returns a boolean indicating whether or not the string is empty length() size() Return the number of characters in the string

string methods at(int index) [int index] Return the character in the string at a given position

string methods compare(string anotherString) Returns an integer, less than zero if calling string 0 if calling string > anotherString are defined in dictionary order (> farther back in dictionary) Can also call compare(char*) compare(int index, int length, string str) – compare whole str to substring of invoker compare(int index, int length, string str, int index2, int length2) – compare substring of str to substring of invoker compare(int index, int length, char* str, int length2 = npos) – compare substring of char array to substring of invoker

string methods substr(int index = 0, int length = npos) Returns substring of invoker

string methods append – Appends another string onto the back of the string Multiple versions append(string str) append(string str, int index, int length) append(char* array) append(char* array, int length) append(int length, char aChar)

string methods insert – Inserts another string into the middle of the invoker Multiple versions insert(int index, string str) insert(int index, string str, int index, int length) insert(int index, char* array) insert(int index, char* array, int length) insert(int index, int length, char aChar)

string methods Replace - Replaces another string into the middle of the invoker Multiple versions replace(int index, int length, string str) replace(int index, int length, string str, int index2, int length2) replace(int index, int length, char* array) replace(int index, int length, char* array, int length) replace(int index, int length, int length2, char aChar)

string methods erase – Deletes a portion of the string erase(int index = 0, int length = npos)

string methods int find(str anotherString) – returns the index of the first occurrence in the invoker of anotherString Versions to look for char*, to start search at different index than 0, to search for substrings of anotherString, to search for a char only int find_first_of(str charsToLookFor) – returns the index of the first occurrence in the invoker of any character in the string charsToLookFor Versions to use char* instead of string, to start search at different index than 0, to search for substrings of charsToLookFor, to search for a char only find_first_not_of - returns the index of the first occurrence in the invoker of any character not in the string charsToLookFor Versions to use char* instead of string, to start search at different index than 0, to search for substrings of charsToLookFor, to search for a char only

string methods rfind - returns the index of the last occurrence in the invoker of anotherString Versions to look for char*, to start search at different index than 0, to search for substrings of anotherString, to search for a char only int find_last_of(str charsToLookFor) – returns the index of the first occurrence in the invoker of any character in the string charsToLookFor Versions to use char* instead of string, to start search at different index than 0, to search for substrings of charsToLookFor, to search for a char only find_last_not_of - returns the index of the first occurrence in the invoker of any character not in the string charsToLookFor Versions to use char* instead of string, to start search at different index than 0, to search for substrings of charsToLookFor, to search for a char only

string methods Iterators – A way of traversing through a datastructure, an element at a time For strings, this is a char at a time begin() – Return an interator to the front of the string end() – Return an iterator to the very back of the string (past the end – usually used for checking if iterated far enough)

Iterators string aString = "helloworld"; string::iterator it = aString.begin(); while (it != aString.end()) { cout << "Iterator: " << *it << endl; it++; } cout << endl; for (int i = 0; i < aString.length(); i++) { cout << "For: " << aString[i] << endl; }

More information g.html

Practice Problems