You are on page 1of 27

COMP6459 – Object Oriented

Programming

Topic 9 – String Class


OUTLINE MATERIALS

Topic 9 – The String Class

• Introduction
• Declaration
• Concatenation, comparison operators, and [ ]
• Functions of the string class
• length, size, empty,
• insert, substr, replace, erase, clear, find
• Useful char functions in the C library <ctype.h>
Topic 9 – The String Class
INTRODUCTION

• Generally speaking, a string is a sequence of characters


• Examples: “Love”, “Binus”, “OOP”.
• Typical desirable operations on strings are:
– Concatenation: “Love”+“Binus”=“LoveBinus”
– Comparisons: “Love”<“OOP” // alphabetical
– Finding/retrieving/modifying/deleting/inserting substrings in
a given string
INTRODUCTION (2)

• C++ has a <string> library


• Include it in your programs when you wish to use strings:
#include <string>
• In this library, a class string is defined and implemented
• It is very convenient and makes string processing easier
than in C
DECLARATION

• The following declaration are all equivalent.


• They declare x to be an object of type string, and
assign the string “high school” to it:
– string s(“Love Binus”);
– string s= “Love Binus”;
– string s; s=“Love Binus”;
OPERATION CONCATENATION

string s1= “Love”;


string s2= “Binus”;
string s3;
s3=s1+s2; • Output:
cout<<“s3=“<<s3<<endl;
s3 =s3+“ Is OK”; • s3=LoveBinus
cout<<“s3=“<<s3<<endl;
• s3= LoveBinus Is OK
OPERATION CONCATENATION (2)

• In s = s1 + s2 + s3; where s is of type string,


– s1 can be
• a string object, or
• a C-style string (a char array or a char pointer),
• a C-style char
• or a double-quoted string,
• or a single-quoted character.
– Same with s2 and s3.
– At least s1 or s2 or s3 must be a string object
CONCATENATION MIXED STYLE

string s1= “Love”;


char s2[]= “Binus”;
char s3[]= {‘I’,’s’,’\0’}; • Output:
char *s4 = “K”; • s=LoveBinus Is OK!
string s= s1+s2+’ ‘+s3+” O”+” “+s4+’!’;
cout<<“s=“<<s<<endl; • s=LoveBinus Is OK!
cout<<“s=“+s<<endl;
CONCATENATION OPERATOR +=

• Assume s1 is a string object.


• The statement
s1 += s2;
is equivalent to
s1=s1+s2;
where s2 can be a string object, a C-style string
variable, a char variable, a double-quoted string, or a
single-quoted char.
COMPARISON OPERATORS

• We can compare two strings s1 and s2 using the following


operators: ==, !=, <, <=, >, >=
• The comparison is alphabetical
• The outcome of each comparison is: true or false
• The comparison works as long as at least s1 or s2 is a string
object. The other string can be a string object, a C-style string
variable, or a double-quoted string.
COMPARISON OPERATORS (2)

string s1= “Love”;


char s2[]= “Binus”; • if( (s3>s1)
char *s3 = “Very”; • cout<<“s3>s1”<<endl;
if (s1<s2) • else
cout<<“s1x<s2”<<endl; • cout<<“s3<=s1”<<endl;
if (s1<“Good”)
cout<<“s1<Good”<,endl; • OUTPUT?
if (“Live” != s1)
cout<<“Live!= s1”<<endl;
THE INDEX OPERATOR []

• If s is a string object, and you wish to obtain the value of the k-th
character in the string, you write: s[k];
• This feature makes string objects appear like arrays of chars.
string s= “Love”;
char c=s[0]; // c is ‘L’
c=s[1]; // c is ‘o’
c=s[2]; // c is ‘v’
STRING LENGTH AND EMPTY

• To obtain the length of a string object s, call the


method length() or size():
int len=s.length( ); //OR
int len=s.size( );

• To check of s is empty, that is has no characters in it


bool s.empty();
SUBSTRING

• Logically, a substring of a string s is a subsequence of consecutive


characters in s
• For example, “nus” is a substring of “Binus”
• If s is a string object, and we want the substring that begins at
position pos and has len characters (where pos and len are of type
int), write:
string s1 = s.substr(pos,len);
• The default value of len is s.length( ) and the default
value for pos is 0
string s1 = s.substr(pos); // s[pos..end-1]
• Example :
string a = “Binus”;
cout << a.substr(2,2); // The result is : nu
INSERTION SUBSTRING

• Suppose s is a string object, and let s1 be another string to be


inserted at position pos of the string of s
• To insert s1, do:
s.insert(pos,s1);
• The argument s1 can be: a string object, a C-style string variable,
or a double-quoted string
• Example :
string s = “nutara”;
s.insert(2,”san”);
cout << s; // The result is : nusantara
REPLACING SUBSTRING

• Suppose s is a string object, and suppose you want to


replace the characters in the range [pos,pos+len) in s by a
string s1.
• To do so, write:
s.replace(pos,len,s1);
• The argument s1 can be: a string object, a C-style string
variable, or a double-quoted string
• Example :
string c = “henphone”;
c.replace(1,2,"and")
cout << c; // The result is : handphone
ERASING SUBSTRING

• Suppose s is a string object, and suppose you want to


delete/erase the characters in the range [pos,pos+len] in s.
• To do so, write:
s.erase(pos,len);
• The default value of len is the s.length( )
s.erase(pos);
• The default value for pos is 0
• To erase the whole string of s, do:
s.clear();
SEARCHING SUBSTRING

• Suppose s is a string object, and suppose you want to search


for a string s1 in s.
• To do so, write int startLoc = s.find(s1);
• This method returns the starting index of the leftmost
occurrence of s1 in s, if any occurrence exits; otherwise, the
method returns the length of s.
• To search starting from a position pos, we write int startLoc =
s.find(s1,pos);
SEARCHING SUBSTRING (2)

• To search for the rightmost occurrence of s1 in s, do


startLoc = s.rfind(s1);
startLoc = s.rfind(s1,pos);
• In all the versions of find and rfind, the argument y can be
a string object, a C-style string variable, double-quoted
string, a char variable, or a single-quoted char.
• Example:
string s = “handphone”;
cout << s.find(“h”)<<endl; // the result is : 0
cout << s.rfind(“h”); // the result is : 5
C-STYLE STRINGS (2)

• You can use c-style strings


• C has a library <strings.h> which provides several string
processing functions
• Some of the more commonly used functions in that library
are presented in the next two slides
• In those functions, most of the arguments are of type char
*str. That can be replaced by char str[];
THE C LIBRARY AND
THE C++ STRING CLASS

C C++
• strcpy • =
• strcat • +=
• strcmp • ==, !=, <, >, <=, >=
• strchr, strstr • find()
• strrchr • rfind()
• strlen • size() or length()
CHAR FUNCTIONS IN C / C++

• The <ctype.h> library in C provides useful functions for single


char variables
• The next slide gives the most common char functions.
• Although the input argument appears to be of type int, it is
actually a char.
EXAMPLE

• int isalnum(int c); //non-zero if c is alphanumeric


• int isalpha(int c); //non-zero if c is alphabetic
• int isdigit(int c); //non-zero if c a digit: 0 to 9
• int islower(int c); //non-zero if c is lower case
• int ispunct(int c); //non-zero if c is punctuation
• int isspace(int c); //non-zero if c is a space char
• int isupper(int c); // non-zero if c is upper case
• int isxdigit(int c); //non-zero if c is hexadecimal
• int tolower(int c); //returns c in lower case
• int toupper(int c); //returns c in upper case
Q&A
References

• Deitel, P., & Deitel, H. (2012). C++ How to Program.8th edition. New
Jersey: Prentice Hall. Chapter 18
• https://www.youtube.com/watch?v=LuFRDp9kKZ4
THANK YOU

You might also like