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

Slides:



Advertisements
Similar presentations
Java
Advertisements

1 StringBuffer & StringTokenizer Classes Chapter 5 - Other String Classes.
Purpose : To convert this string to a new character array. Return Type : char[ ] Parameters : none Declaration : public char[ ] toCharArray() Returns.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 9 Strings.
Strings in Java 1. strings in java are handled by two classes String &
Chapter 7 Strings F To process strings using the String class, the StringBuffer class, and the StringTokenizer class. F To use the String class to process.
1 Working with String Duo Wei CS110A_ Empty Strings An empty string has no characters; its length is 0. Not to be confused with an uninitialized.
1 Strings and Text I/O. 2 Motivations Often you encounter the problems that involve string processing and file input and output. Suppose you need to write.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 8 Strings.
Java Programming Strings Chapter 7.
Strings An extension of types A class that encompasses a character array and provides many useful behaviors Chapter 9 Strings are IMMUTABLE.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 9 Strings and Text I/O.
©2004 Brooks/Cole Chapter 7 Strings and Characters.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 8 Strings and Text.
String class  Construct a string  String str = new String(“welcome”);  Char[] charr = {‘G’, ‘o’, ‘o’, ‘d’};  String mes = new String(charr);  A full.
Chapter 9 Characters and Strings. Topics Character primitives Character Wrapper class More String Methods String Comparison String Buffer String Tokenizer.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 9 Characters and Strings (sections ,
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 9 Strings and Text.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 8 Strings and Text.
CSM-Java Programming-I Spring,2005 String Handling Lesson - 6.
StringBuffer class  Alternative to String class  Can be used wherever a string is used  More flexible than String  Has three constructors and more.
Fundamental Programming Structures in Java: Strings.
COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Odds and Ends Strings (from Chapter 9) StringTokenizer.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 9 Characters and Strings (sections ,
Session 5 java.lang package Using array java.io package: StringTokenizer, ArrayList, Vector Using Generic.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 7 Strings.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 9 Strings and Text.
String & Composition. Agenda This keyword. String class. String operations. Composition.
Chapter 7 Strings  Use the String class to process fixed strings.  Use the StringBuffer class to process flexible strings.  Use the StringTokenizer.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 8 Strings and Text.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 8 Strings 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.
An Introduction to Java Programming and Object-Oriented Application Development Chapter 7 Characters, Strings, and Formatting.
Strings Chapter 7 CSCI CSCI 1302 – Strings2 String Comparisons Compare string contents with the equals(String s) method not == String s0 = “ Java”;
Chapter 7: Characters, Strings, and the StringBuilder.
String Handling StringBuffer class character class StringTokenizer class.
Strings Chapter 7 CSCI CSCI 1302 – Strings2 Outline Introduction The String class –Constructing a String –Immutable and Canonical Strings –String.
Geoff Holmes Date Math Weighted Distr Strings String methods Tokenizers System Examples Utility Classes (Chapter 17) import java.util.*;
Jaeki Song JAVA Lecture 07 String. Jaeki Song JAVA Outline String class String comparisons String conversions StringBuffer class StringTokenizer class.
1 The String Class F Constructing a String: F Obtaining String length and Retrieving Individual Characters in a string F String Concatenation (concat)
String Definition A String is a set of characters that behaves as a single unit. The characters in a String include upper-case and lower-case letters,
CHAPTER 8 File Input Output Part 1: String. The String Class  Constructing a String: String message = "Welcome to Java“; String message = new String("Welcome.
Chapter 5 Programming with Objects and Classes OO Programming Concepts OO Programming Concepts Declaring and Creating Objects Declaring and Creating Objects.
G51PR1 Introduction to Programming I University of Nottingham Unit 8 : Strings.
Strings and Related Classes String and character processing Class java.lang.String Class java.lang.StringBuffer Class java.lang.Character Class java.util.StringTokenizer.
String Definition A string is a set of characters that behaves as a single unit. The characters in a string include upper-case and lower- case letters,
Java String 1. String String is basically an object that represents sequence of char values. An array of characters works same as java string. For example:
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 10 Thinking in Objects.
Strings A string is a sequence of characters that is treated as a single value. Strings are objects. We have been using strings all along. For example,
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 7 Strings Chapter.
Strings Chapter 7 CSCI CSCI 1302 – Strings2 Outline Introduction The String class –Constructing a String –Immutable and Canonical Strings –String.
Chapter 9 Strings and Text I/O
String and StringBuffer classes
Strings, Characters and Regular Expressions
Strings, StringBuilder, and Character
Strings.
String Handling in JAVA
Chapter 8 Strings and Text I/O
Chapter 7: Strings and Characters
MSIS 655 Advanced Business Applications Programming
Chapter 9 Strings and Text I/O
Chapter 9 Strings.
Lecture 07 String Jaeki Song.
Chapter 10 Thinking in Objects Part 2
Chapter 7 Strings Processing strings using the String class, the StringBuffer class, and the StringTokenizer class. Use the String class to process fixed.
In Java, strings are objects that belong to class java.lang.String .
Unit-2 Objects and Classes
Presentation transcript:

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

The String Class F Declaring a String: –String message = "Welcome to Java!" –String message = new String("Welcome to Java!“); –String s = new String(); F String Comparisons (equals, compareTo) F String Concatenation (concat) F Substrings (substring(index), substring(start, end)) F String Length (length()) F Retrieving Individual Characters in a string

String Comparisons F 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 }

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).

String Comparisons, cont. F 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

Substrings String is an immutable class; its values cannot be changed individually. String s1 = "Welcome to Java"; String s2 = s1.substring(0,10) + "HTML";

String Concatenation String s3 = s1.contact(s2); String s3 = s1 + s2;

Finding String Length Finding string length using the length() method: message = "Welcome"; message.length() (returns 7 )

Retrieving Individual Characters in a String  Do not use message[0]  Use message.charAt(index)  Index starts from 0

String Conversions The 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: F toLowerCase F toUpperCase F trim F replace(oldChar, newChar)

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’.

Example 7.1 Finding Palindromes F Objective: Checking whether a string is a palindrome: a string that reads the same forward and backward. CheckPalindrome Run

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 contents into a string buffer. However, the value of a string is fixed once the string is created.

StringBuffer Constructors F public StringBuffer() No characters, initial capacity 16 characters. F public StringBuffer(int length) No characters, initial capacity specified by the length argument. F public StringBuffer(String str) Represents the same sequence of characters as the string argument. Initial capacity 16 plus the length of the string argument.

Appending New Contents into a String Buffer StringBuffer strBuf = new StringBuffer(); strBuf.append("Welcome"); strBuf.append(' '); strBuf.append("to"); strBuf.append(' '); strBuf.append("Java");

The StringTokenizer Class Constructors F StringTokenizer(String s, String delim, boolean returnTokens) F StringTokenizer(String s, String delim)  StringTokenizer(String s)

The StringTokenizer Class Methods F boolean hasMoreTokens() F String nextToken() F String nextToken(String delim)

Example 7.4 Testing StringTokenizer F Objective: Using a string tokenizer, retrieve words from a string and display them on the console. TestStringTokenizerRun

Command-Line Parameters class TestMain { public static void main(String[] args) {... } } java TestMain arg0 arg1 arg2... argn

Processing Command-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.

Example 7.5 Using Command-Line Parameters F Objective: Write a program that will perform binary operations on integers. The program receives three parameters: an operator and two integers. Calculator java Calculator java Calculator Run java Calculator / 2 3