27
Chapter 9 Characters and Strings

Chapter 9 Characters and Strings. Topics Character primitives Character Wrapper class More String Methods String Comparison String Buffer String Tokenizer

  • View
    246

  • Download
    2

Embed Size (px)

Citation preview

Page 1: Chapter 9 Characters and Strings. Topics Character primitives Character Wrapper class More String Methods String Comparison String Buffer String Tokenizer

Chapter 9

Characters and Strings

Page 2: Chapter 9 Characters and Strings. Topics Character primitives Character Wrapper class More String Methods String Comparison String Buffer String Tokenizer

Topics

• Character primitives

• Character Wrapper class

• More String Methods

• String Comparison

• String Buffer

• String Tokenizer

Page 3: Chapter 9 Characters and Strings. Topics Character primitives Character Wrapper class More String Methods String Comparison String Buffer String Tokenizer

Character Data

• In Java, single characters are represented using the primitive data type char.

• Character constants are written as symbols enclosed in single quotes:

char ch1 = 'X';

Page 4: Chapter 9 Characters and Strings. Topics Character primitives Character Wrapper class More String Methods String Comparison String Buffer String Tokenizer

Character Encoding

• Characters are stored in memory as integer values• Each character has a unique "code" that is used to

represent it.• Having a standard encoding allows different

computers to share information easily.• Several encoding schemes have been used

– ASCII

– EBCDIC (not used any more)

– Unicode

Page 5: Chapter 9 Characters and Strings. Topics Character primitives Character Wrapper class More String Methods String Comparison String Buffer String Tokenizer

ASCII Codes

• ASCII stands for American Standard Code for Information Interchange.

• ASCII is one of the document coding schemes widely used today.– each character represented by 8 bits ( values 0-255)

• ASCII works well for English-language documents because all characters and punctuation marks are included in the ASCII codes.

• ASCII does not represent the full character sets of other languages.

Page 6: Chapter 9 Characters and Strings. Topics Character primitives Character Wrapper class More String Methods String Comparison String Buffer String Tokenizer

Unicode

• The Unicode Worldwide Character Standard (Unicode) supports the interchange, processing, and display of the written texts of diverse languages.

• Java uses the Unicode standard for representing char constants.

• Each character is represented with 2 bytes (16 bits).

Page 7: Chapter 9 Characters and Strings. Topics Character primitives Character Wrapper class More String Methods String Comparison String Buffer String Tokenizer

Character Class

• Like the other primitive types, there is a wrapper class for the char type.

• Useful Methods– charValue( ) returns a char that is the

value stored in the Character– getNumericValue( ) returns the Unicode

value for the char

Page 8: Chapter 9 Characters and Strings. Topics Character primitives Character Wrapper class More String Methods String Comparison String Buffer String Tokenizer

Static CharacterMethods

• boolean methods– isDigit( char)– isLetter( char)– isSpace( char)– isUpperCase( char)– isLowerCase(char)

• other methods– char toLowerCase( char)– toUpperCase( char)

Page 9: Chapter 9 Characters and Strings. Topics Character primitives Character Wrapper class More String Methods String Comparison String Buffer String Tokenizer

Strings

• A string is a sequence of characters that is treated as a single value.

• Instances of the String class are used to represent strings in Java.

• We access individual characters of a string by calling the charAt method of the String object.

Page 10: Chapter 9 Characters and Strings. Topics Character primitives Character Wrapper class More String Methods String Comparison String Buffer String Tokenizer

Strings

• Each character in a string has an index that we can use to access the character.

• We know for learning about arrays that Java uses zero-based indexing– the first character’s index is 0, the second is 1, and so

on.

• To refer to the first character of the word name, we say

name.charAt(0)

Page 11: Chapter 9 Characters and Strings. Topics Character primitives Character Wrapper class More String Methods String Comparison String Buffer String Tokenizer

String Indexing

• An indexed expression is used to refer to individual characters in a string.

Page 12: Chapter 9 Characters and Strings. Topics Character primitives Character Wrapper class More String Methods String Comparison String Buffer String Tokenizer

Strings

• Since String is a class, we can create an instance of a class by using the new method.

• The statements we have used so far, such asString name1 = “Kona”;

• works as a shorthand for String name1 = new String(“Kona”);

• But this shorthand works for the String class only.

Page 13: Chapter 9 Characters and Strings. Topics Character primitives Character Wrapper class More String Methods String Comparison String Buffer String Tokenizer

Other String Methods

• String toLowerCase()• String toUpperCase()• String replace( char oldChar,

char newChar);

• and many more

Page 14: Chapter 9 Characters and Strings. Topics Character primitives Character Wrapper class More String Methods String Comparison String Buffer String Tokenizer

String Comparison

• String comparison may be done in several ways. – The methods equals and equalsIgnoreCase compare

string values; one is case-sensitive and one is not.– The method compareTo returns an int value:

• Zero (0) if the strings are equal. • A negative integer if the first string is less than

the second.• A positive integer if the first string is greater

than the second.

Page 15: Chapter 9 Characters and Strings. Topics Character primitives Character Wrapper class More String Methods String Comparison String Buffer String Tokenizer

String Equality

• Comparing String objects is similar to comparing other objects.

• The equality test (==) is true if the contents of the variables are the same.– For a reference data type, the equality test is true if

both variables refer to the same object, because they both contain the same address.

• The equals method is true if the String objects to which the two variables refer contain the same string value.– the same sequence of characters

Page 16: Chapter 9 Characters and Strings. Topics Character primitives Character Wrapper class More String Methods String Comparison String Buffer String Tokenizer

Equality Testing

• The difference between the equality test and the equals method.

Page 17: Chapter 9 Characters and Strings. Topics Character primitives Character Wrapper class More String Methods String Comparison String Buffer String Tokenizer

Equality Testing

Page 18: Chapter 9 Characters and Strings. Topics Character primitives Character Wrapper class More String Methods String Comparison String Buffer String Tokenizer

Equality Testing for Strings

• As long as a new String object is created using the new operator, the rule for comparing objects applies to comparing strings.

String str = new String (“Java”);

• If the new operator is not used, a String reference refers to the literal string constant.

String str = “Java”;

– There will be only one copy of any literal string

Page 19: Chapter 9 Characters and Strings. Topics Character primitives Character Wrapper class More String Methods String Comparison String Buffer String Tokenizer

Memory Diagram

• The difference between using and not using the new operator for String.

Page 20: Chapter 9 Characters and Strings. Topics Character primitives Character Wrapper class More String Methods String Comparison String Buffer String Tokenizer

Immutability

• When a String object is created, it cannot be changed.– Look at the documentation - there are no

mutator methods– We say Strings are immutable.

• Manipulating the content of a String requires the creation of a new String object.– If you are doing a lot of manipulation this is not

very efficient.

Page 21: Chapter 9 Characters and Strings. Topics Character primitives Character Wrapper class More String Methods String Comparison String Buffer String Tokenizer

StringBuffer Class

• Manipulating the content of a string, such as replacing a character, appending a string with another string, deleting a portion of a string, and so on, may be accomplished more efficiently using the StringBuffer class.

Page 22: Chapter 9 Characters and Strings. Topics Character primitives Character Wrapper class More String Methods String Comparison String Buffer String Tokenizer

StringBuffer Example

StringBuffer word = new StringBuffer(“Java”);

word.setCharAt(0, ‘D’);

word.setCharAt(1, ‘i’ );

• changes the string from “Java” to “Diva.”

Page 23: Chapter 9 Characters and Strings. Topics Character primitives Character Wrapper class More String Methods String Comparison String Buffer String Tokenizer

Ch9ReplaceVowelsWithX.java

• This example reads a sentence and replaces all vowels in the sentence with the character X. tempStringBuffer= new StringBuffer(inSentence);

numberOfCharacters = tempStringBuffer.length();

for (int index = 0; index < numberOfCharacters; index++) {

if ( isVowel( letter) { tempStringBuffer.setCharAt(index,'X');

Page 24: Chapter 9 Characters and Strings. Topics Character primitives Character Wrapper class More String Methods String Comparison String Buffer String Tokenizer

StringBuffer Operations

• We cannot input StringBuffer objects. – We must input String objects and convert them to

StringBuffer objects.

• We use the append method to append a String or StringBuffer object to the end of a StringBuffer object. – The method append can also take an argument of the

primitive data type.– Any primitive data type argument is converted to a

string before it is appended to a StringBuffer object.

Page 25: Chapter 9 Characters and Strings. Topics Character primitives Character Wrapper class More String Methods String Comparison String Buffer String Tokenizer

StringBuffer Insertion

• We can insert a string at a specified position by using the insert method.

<StringBuffer>.insert(<insertIndex>, <value>);

where <insertIndex> must be greater than or equal to 0 and less than or equal to the length of <StringBuffer>, and the <value> can be an object or a primitive.

• For example, executingStringBuffer str =

new StringBuffer(“Java is great”);str.insert(8, “really”);

changes the string "Java is great" to "Java is really great".

Page 26: Chapter 9 Characters and Strings. Topics Character primitives Character Wrapper class More String Methods String Comparison String Buffer String Tokenizer

String Tokenizer

• A StringTokenizer object can be used to break a string up into smaller substrings (tokens)

• By default, the tokenizer separates the string at white space

• The default StringTokenizer constructor takes the original string to be separated as a parameterStringTokenizer st = new StringTokenizer( text);

• An alternate constructor allows you to specify different separators (delimiters).StringTokenizer st = new StringTokenizer( text, delims);

Page 27: Chapter 9 Characters and Strings. Topics Character primitives Character Wrapper class More String Methods String Comparison String Buffer String Tokenizer

String Tokenizer Methods

• The hasMoreTokens method returns true if there are more tokens in the string.

• Each call to the nextToken method returns the next token in the string.

• If you need to know how many tokens there are, you can call countTokens