The string Class - PowerPoint PPT Presentation

1 / 10
About This Presentation
Title:

The string Class

Description:

A string may simply be declared, or it may be declared and initialized by another string. ... Examples. PathName.cpp. ReverseStyle.cpp ... – PowerPoint PPT presentation

Number of Views:34
Avg rating:3.0/5.0
Slides: 11
Provided by: peopl93
Category:
Tags: class | pathname | string

less

Transcript and Presenter's Notes

Title: The string Class


1
The string Class
  • Lecture 16 Fri, Oct 3, 2003

2
String Declarations and Initialization
  • A string may simply be declared, or it may be
    declared and initialized by another string.
  • If it is not explicitly initialized, then it is
    set to the empty string.

string s // Empty string string t
Hello // Initialized
3
String Operators
  • Assignment
  • Input and output gtgt, ltlt
  • Comparison , !, lt, gt, lt, gt
  • Concatenation
  • Subscript

4
Member Functions
  • A data type may have member functions.
  • A member function is a function that can be
    invoked only by means of an object of that type.
  • Typically, a member function acts on the object
    that invokes it.

Object.Member-function()
5
Member Functions
  • We have already used the string member function
    size() to get the size of a string.

6
String Member Functions
  • int length()
  • int size()
  • Get the number of characters in the string.
  • char c_str()
  • Get the same string as a C-type null-terminated
    string.

7
String Member Functions
  • int find(string str)
  • Get the index of a specified string str as a
    substring of the invoking string return -1 if
    the specified string cannot be found.
  • void append(string str)
  • Append a specified string str to the end of the
    invoking string.

8
String Member Functions
  • void insert(int i, string str)
  • Insert a specified string str into the invoking
    string, beginning at a specified position i.
  • string substr(int i, int j)
  • Get the substring of a specified length j,
    beginning in a specified position i.

9
String Member Functions
  • void erase(int i, int j)
  • Remove a specified number j of characters from
    the invoking string, beginning in a specified
    position i.
  • void clear()
  • Remove all characters from the string.
  • bool empty()
  • Determine whether the string is empty. Return
    true if the string has length 0 false otherwise.

10
Examples
  • PathName.cpp
  • ReverseStyle.cpp
Write a Comment
User Comments (0)
About PowerShow.com