Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 7: Strings and Characters

Similar presentations


Presentation on theme: "Chapter 7: Strings and Characters"— Presentation transcript:

1 Chapter 7: Strings and Characters
Object-Oriented Program Development Using Java: A Class-Centered Approach

2

3 String Processing Strings are manipulated using:
Standard String class methods Character-at-a-time methods provided by the Character class

4 String Equality Never use the equality operator, == Use:
Compares values stored in reference variables used to access strings Use: compareTo() equals()

5 Major String methods String (String str)
Constructor: creates a new string object with the same characters as str. char charAt (int index) Returns the character at the specified index. int compareTo (String str) Returns an integer indicating if this string is lexically before (a negative return value), equal to (returns 0) or lexically after (returns a positive integer), the string str.

6 Major String methods String concat (String str)
Returns a new string object consisting of this string concatenated with str. boolean equals (String str) Returns true if this string contains the same characters as str (including case) and false otherwise. boolean equalsIgnoreCase (String str) Returns true if this string contains the same characters as str (without regard to case) and false otherwise.

7 Major String methods (cont.)
int length (String str) Returns the number of characters in this string. String replace (char oldChar, char newChar) Returns new string identical to original except every character oldChar is replaced with the character newChar String substring (int offset, int endIndex) Returns new string that is a subset of this string starting at index offset and extending through endIndex-1.

8 Major String methods (last slide –finally!
String toLowerCase( ) Returns new string identical to original except all uppercase letters are converted to their lowercase equivalent. String toUpperCase( ) Returns new string identical to original except all lowercase letters are converted to their uppercase equivalent. .

9 The StringBuffer Class
Implemented as a mutable sequence of characters Can be modified Does not provide a set of methods for: Comparing strings Locating characters and substrings within a string Created and stored in a buffer having a set character capacity

10 The StringBuffer Class (continued)
String class Preferred class for displaying or comparing strings that tend to remain constant StringBuffer class Should be used for applications that require: Individual character additions Modifications Multiple text edits

11

12

13 Common Programming Errors
Breaking a string across two or more lines Not remembering that the first character in a string is located at position 0, not 1 Not remembering that the number returned by the length() method is one more than the position number of the string’s last character Using the == operator to compare two strings Rather than the compareTo() or equals() methods

14 Common Programming Errors (continued)
Not specifying one position beyond the desired character to be extracted using the substring() method

15 Summary Strings constructed from the String class are immutable
More commonly used for constructing strings for input and output purposes StringBuffer class Used when characters within a string need to be replaced, inserted, or deleted on a regular basis

16 Summary (continued) Strings can be manipulated using either:
Methods of the class they are objects of Using general-purpose string and character methods


Download ppt "Chapter 7: Strings and Characters"

Similar presentations


Ads by Google