chapter 7 strings f processing strings using the string class, the stringbuffer class, and the...

21
Chapter 7 Strings Processing strings using the String class, the StringBuffer class, and the StringTokenizer class. Use the String class to process fixed strings. Use the StringBuffer class to process flexible strings. Use the StringTokenizer class to extract tokens from a string. Use the command-line arguments.

Upload: aja-thompson

Post on 14-Dec-2015

237 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: Chapter 7 Strings F Processing strings using the String class, the StringBuffer class, and the StringTokenizer class. F Use the String class to process

Chapter 7 Strings

Processing strings using the String class, the StringBuffer class, and the StringTokenizer class.

Use the String class to process fixed strings. Use the StringBuffer class to process flexible

strings. Use the StringTokenizer class to extract tokens

from a string. Use the command-line arguments.

Page 2: Chapter 7 Strings F Processing strings using the String class, the StringBuffer class, and the StringTokenizer class. F Use the String class to process

The String Class Declaring a String:

– String message = "Welcome to Java!"– String message = new String("Welcome to Java!“);

– String s = new String(); String Comparisons (equals, compareTo) String Concatenation (concat) Substrings (substring(index), substring(start, end)) String Length (length()) Retrieving Individual Characters in a string

Page 3: Chapter 7 Strings F Processing strings using the String class, the StringBuffer class, and the StringTokenizer class. F Use the String class to process

String Comparisons equals

String s1 = "Welcome";String s2 = "welcome";

if (s1.equals(s2)) { // s1 and s2 have the same contents } if (s1 == s2)

{ // s1 and s2 have the same reference }

Page 4: Chapter 7 Strings F Processing strings using the String class, the StringBuffer class, and the StringTokenizer class. F Use the String class to process

Strings are immutable Strings are immutable. The contents of a string cannot be changed. To improve efficiency and save memory, Java Virtual Machine makes a great effort to identify the identical strings and store them in the same memory location, but it does not guarantee that all of the same strings are stored in the same memory location. Therefore, you must use the equals method to test whether two strings have the same contents, and the == operator to test whether the two strings have the same references (that is, point to the same memory location).

Page 5: Chapter 7 Strings F Processing strings using the String class, the StringBuffer class, and the StringTokenizer class. F Use the String class to process

String Comparisons, cont. compareTo(Object object)

String s1 = "Welcome";String s2 = "welcome";

if (s1.compare(s2) > 0) { // s1 is greater than s2 } else if (s1.compare(s2 == 0) { // s1 and s2 have the same reference } else // s1 is less than s2

Page 6: Chapter 7 Strings F Processing strings using the String class, the StringBuffer class, and the StringTokenizer class. F Use the String class to process

Substrings

String is an immutable class; its valuescannot be changed individually.

String s1 = "Welcome to Java";

String s2 = s1.substring(0,10) + "HTML";

Page 7: Chapter 7 Strings F Processing strings using the String class, the StringBuffer class, and the StringTokenizer class. F Use the String class to process

String Concatenation

String s3 = s1.contact(s2);

String s3 = s1 + s2;

Page 8: Chapter 7 Strings F Processing strings using the String class, the StringBuffer class, and the StringTokenizer class. F Use the String class to process

Finding String Length

Finding string length using the length() method:

message = "Welcome";

message.length() (returns 7)

Page 9: Chapter 7 Strings F Processing strings using the String class, the StringBuffer class, and the StringTokenizer class. F Use the String class to process

Retrieving Individual Characters in a String

Do not use message[0]

Use message.charAt(index)

Index starts from 0

Page 10: Chapter 7 Strings F Processing strings using the String class, the StringBuffer class, and the StringTokenizer class. F Use the String class to process

String ConversionsThe contents of a string cannot be changed once the string is created. But you can convert a string to a new string using the following methods:

toLowerCase toUpperCase trim replace(oldChar, newChar)

Page 11: Chapter 7 Strings F Processing strings using the String class, the StringBuffer class, and the StringTokenizer class. F Use the String class to process

Convert char and numbers to Strings

The String class provides several static valueOf methods for converting a character, an array of characters, and numeric values to strings. These methods have the same name valueOf with different argument types char, char[], double, long, int, and float. For example, to convert a double value to a string, use String.valueOf(5.44). The return value is string consists of characters ‘5’, ‘.’, ‘4’, and ‘4’.

Page 12: Chapter 7 Strings F Processing strings using the String class, the StringBuffer class, and the StringTokenizer class. F Use the String class to process

Example 7.1Finding Palindromes

Objective: Checking whether a string is a palindrome: a string that reads the same forward and backward.

CheckPalindromeCheckPalindrome RunRun

Page 13: Chapter 7 Strings F Processing strings using the String class, the StringBuffer class, and the StringTokenizer class. F Use the String class to process

The StringBuffer Class

The StringBuffer class is an alternative to the String class. In general, a string buffer can be used wherever a string is used.

StringBuffer is more flexible than String. You can add, insert, or append new contentsinto a string buffer. However, the value ofa string is fixed once the string is created.

Page 14: Chapter 7 Strings F Processing strings using the String class, the StringBuffer class, and the StringTokenizer class. F Use the String class to process

StringBuffer Constructors public StringBuffer()

No characters, initial capacity 16 characters.

public StringBuffer(int length)No characters, initial capacity specified by the length argument.

public StringBuffer(String str)Represents the same sequence of charactersas the string argument. Initial capacity 16plus the length of the string argument.

Page 15: Chapter 7 Strings F Processing strings using the String class, the StringBuffer class, and the StringTokenizer class. F Use the String class to process

Appending New Contentsinto a String Buffer

StringBuffer strBuf = new StringBuffer();strBuf.append("Welcome");strBuf.append(' ');strBuf.append("to");strBuf.append(' ');strBuf.append("Java");

Page 16: Chapter 7 Strings F Processing strings using the String class, the StringBuffer class, and the StringTokenizer class. F Use the String class to process

The StringTokenizer Class Constructors

StringTokenizer(String s, String delim, boolean returnTokens)

StringTokenizer(String s, String delim)

StringTokenizer(String s)

Page 17: Chapter 7 Strings F Processing strings using the String class, the StringBuffer class, and the StringTokenizer class. F Use the String class to process

The StringTokenizer Class Methods

boolean hasMoreTokens()

String nextToken()

String nextToken(String delim)

Page 18: Chapter 7 Strings F Processing strings using the String class, the StringBuffer class, and the StringTokenizer class. F Use the String class to process

Example 7.4Testing StringTokenizer

Objective: Using a string tokenizer, retrieve words from a string and display them on the console.

TestStringTokenizerTestStringTokenizer RunRun

Page 19: Chapter 7 Strings F Processing strings using the String class, the StringBuffer class, and the StringTokenizer class. F Use the String class to process

Command-Line Parameters

class TestMain

{

public static void main(String[] args)

{ ... }

}

java TestMain arg0 arg1 arg2 ... argn

Page 20: Chapter 7 Strings F Processing strings using the String class, the StringBuffer class, and the StringTokenizer class. F Use the String class to process

ProcessingCommand-Line Parameters

In the main method, get the arguments from args[0], args[1], ..., args[n], which corresponds to arg0, arg1, ..., argn in the command line.

Page 21: Chapter 7 Strings F Processing strings using the String class, the StringBuffer class, and the StringTokenizer class. F Use the String class to process

Example 7.5Using Command-Line Parameters

Objective: Write a program that will perform binary operations on integers. The program receives three parameters: an operator and two integers.

CalculatorCalculator

java Calculator + 2 3

java Calculator - 2 3

RunRun java Calculator / 2 3