350 likes | 471 Views
Chapter 12: String Manipulation. Introduction to Programming with C++ Fourth Edition. Objectives. Determine the number of characters contained in a string Remove characters from a string Access characters contained in a string Replace characters in a string
E N D
Chapter 12:String Manipulation Introduction to Programming with C++ Fourth Edition
Objectives • Determine the number of characters contained in a string • Remove characters from a string • Access characters contained in a string • Replace characters in a string • Insert characters within a string Introduction to Programming with C++, Fourth Edition
Objectives (continued) • Search a string for another string • Compare a portion of a string variable’s contents to another string • Duplicate a character within a string variable • Concatenate strings Introduction to Programming with C++, Fourth Edition
Manipulating Strings • Programs often need to manipulate (process) string data • Verify that an inventory part number begins with a specific letter • Determine whether the last three characters in an employee number are valid Introduction to Programming with C++, Fourth Edition
Determining the Number of Characters Contained in aString • length() function: determines the number of characters contained in a string • Returns an unsigned integer; you need to use the int type cast when assigning the return value to an int variable • string.length() Introduction to Programming with C++, Fourth Edition
Syntax and Examples of the length() Function Introduction to Programming with C++, Fourth Edition
Syntax and Examples of the length() Function (continued) Introduction to Programming with C++, Fourth Edition
Removing Characters from a String • An application may need to remove characters from an item of data entered by the user • Remove a dollar sign from the beginning of a sales amount • Remove a percent sign from the end of a tax rate • Use the erase() function to remove one or more characters located anywhere in a string variable • string.erase(subscript[, count]) Introduction to Programming with C++, Fourth Edition
Removing Characters from a String (continued) • Each character in a string is assigned a unique number (subscript) that indicates the character’s position in the string (starting from 0) • subscript is the subscript of the first character you want removed from the string • count is the number of characters you want removed • If you omit count, erase() removes all characters from subscript to the end of the string Introduction to Programming with C++, Fourth Edition
Syntax and Examples of the erase() Function Introduction to Programming with C++, Fourth Edition
Accessing Characters Contained in a String • An application may need to access one or more characters contained in a string • Determine whether the letter “K” appears as the third character in a string • Display only the string’s first five characters • Use the substr() function to access any number of characters contained in a string variable • string.substr(subscript[, count]) Introduction to Programming with C++, Fourth Edition
Accessing Characters Contained in a String (continued) • substr() function contains two arguments: • Subscript is the subscript of the first character you want to access in the string (starting from 0) • The count argument, which is optional, specifies the number of characters you want to access • Returns a string with count number of characters, beginning with the character whose subscript is specified by subscript • If you omit count, it returns all the characters from the subscript position through the end of the string Introduction to Programming with C++, Fourth Edition
Syntax and Examples of the substr() Function Introduction to Programming with C++, Fourth Edition
Replacing Characters in a String • Use the replace() function to replace a sequence of characters in a string variable with another sequence of characters • Use the replace() function to replace area code “800” with area code “877” in a phone number • string.replace(subscript, count, replacementString) Introduction to Programming with C++, Fourth Edition
Replacing Characters in a String (continued) • string is the name of a string variable that contains one or more characters you want to replace • subscript specifies where to begin replacing characters in the string • count indicates the number of characters to replace • replacementString contains the string that will replace the characters in the string Introduction to Programming with C++, Fourth Edition
Syntax and Examples of the Replace() Function Introduction to Programming with C++, Fourth Edition
Inserting Characters within a String • Use the insert() function to insert characters within a string variable • Insert an employee’s middle initial within his or her name • Insert parentheses around the area code in a phone number • string.insert(subscript, insertString) Introduction to Programming with C++, Fourth Edition
Inserting Characters within a String (continued) • subscript specifies where in the string you want the insertString inserted (0 is the beginning) • Returns a string with the appropriate characters inserted Introduction to Programming with C++, Fourth Edition
Syntax and Examples of the insert() Function Introduction to Programming with C++, Fourth Edition
Searching a String • Use the find() function to search a string variable to determine whether it contains a specific sequence of characters • Determine whether the area code “312” appears in a phone number • Determine whether the street name “Elm Street” appears in an address • string.find(searchString, subscript) Introduction to Programming with C++, Fourth Edition
Searching a String (continued) • string: the string variable you want to search • searchString: thestring you are searching for • subscript: the starting position for the search • Search for searchString in string, starting with the character in position subscript in the string • If searchString is found in the string, return the beginning position of searchString in the string • Return –1 if the searchString is not found Introduction to Programming with C++, Fourth Edition
Syntax and Examples of the find() Function Introduction to Programming with C++, Fourth Edition
Syntax and Examples of the find() Function (continued) Introduction to Programming with C++, Fourth Edition
Comparing a Portion of a stringVariable’s Contents to Another String • You can use comparison operators with strings • Sometimes you need to compare a portion of one string • Compare the last two characters in the employNum variable to “12” to determine if the employee works in the Accounting department, which has a department code of 12 • Use the compare() function to compare a portion of a string variable’s contents to another string • string1.compare(subscript, count, string2) Introduction to Programming with C++, Fourth Edition
Comparing a Portion of a stringVariable’s Contents to Another String (continued) • string1 and string2 are strings to compare • string1 is a string variable and string2 can be a string literal constant or a string variable • subscript specifies where in string1 the comparison should begin • count indicates the number of characters in string1 to compare to the characters in string2 Introduction to Programming with C++, Fourth Edition
Syntax and Examples of the compare() Function Introduction to Programming with C++, Fourth Edition
Syntax and Examples of the compare() Function (continued) Introduction to Programming with C++, Fourth Edition
Duplicating a Character within a string Variable • Use the assign() function to duplicate one character a specified number of times, then assign the resulting string to a string variable • string.assign(count, character) Introduction to Programming with C++, Fourth Edition
Duplicating a Character within a string Variable (continued) • string is the name of a string variable that will store the duplicated characters • count is either a numeric literal constant or the name of a numeric variable • Indicates the number of times you want to duplicate the character specified in the function’s character argument • character can be either a character literal constant or the name of a char variable Introduction to Programming with C++, Fourth Edition
Syntax and Examples of the assign() Function Introduction to Programming with C++, Fourth Edition
Concatenating Strings • Connecting (or linking) strings together is called concatenating • Use the concatenation operator (+) to concatenate strings Introduction to Programming with C++, Fourth Edition
Examples of Using the Concatenation Operator Introduction to Programming with C++, Fourth Edition
Summary • Programs often need to manipulate (process) string data • length() determines the number of characters contained in a string variable • erase() removes characters in a string • substr() accesses characters in a string • replace() replaces a sequence of characters in a string Introduction to Programming with C++, Fourth Edition
Summary (continued) • insert() inserts characters in a string • find() searches a string to determine whether it contains a specific sequence of characters • compare() compares a portion of a string variable’s contents to another string • assign() duplicates one character a specified number of times, then assigns the resulting string to a string variable • Use + to concatenate (join) strings Introduction to Programming with C++, Fourth Edition