1 String Buffer & String Tokenizer Overview l Overview of String Buffer class and Methods l Overview of String Tokenizer class and methods l Preview: Notions.

Slides:



Advertisements
Similar presentations
1 StringBuffer & StringTokenizer Classes Chapter 5 - Other String Classes.
Advertisements

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.
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.
Chapter 7 Strings F Processing strings using the String class, the StringBuffer class, and the StringTokenizer class. F Use the String class to process.
©2004 Brooks/Cole Chapter 7 Strings and Characters.
Chapter 3, More on Classes & Methods Clark Savage Turner, J.D., Ph.D. Copyright 2003 CSTurner, from notes and text.
Chapter 7: The String class We’ll go through some of this quickly!
Chapter 9 Characters and Strings. Topics Character primitives Character Wrapper class More String Methods String Comparison String Buffer String Tokenizer.
String Tokenization What is String Tokenization?
CSM-Java Programming-I Spring,2005 String Handling Lesson - 6.
23-Jun-15 Strings, Etc. Part I: String s. 2 About Strings There is a special syntax for constructing strings: "Hello" Strings, unlike most other objects,
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.
1 StringTokenizer and StringBuffer classes Overview l StringTokenizer class l Some StringTokenizer methods l StringTokenizer examples l StringBuffer class.
Strings, Etc. Part I: Strings. About Strings There is a special syntax for constructing strings: "Hello" Strings, unlike most other objects, have a defined.
28-Jun-15 String and StringBuilder Part I: String.
COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Odds and Ends Strings (from Chapter 9) StringTokenizer.
Strings Edward J. Biebel. Strings Strings are fundamental part of all computing languages. At the basic level, they are just a data structure that can.
Session 5 java.lang package Using array java.io package: StringTokenizer, ArrayList, Vector Using Generic.
Java Utility Classes CS 21b. Some Java Utility Classes Vector Hashtable StringTokenizer * import java.util.*;
Java Methods A & AB Chapter 10 - Strings. Ch 10 Goals Understand Strings indepth Learn strategies to deal with the immutability of Strings Learn how to.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 7 Strings.
Chapter 7 Strings  Use the String class to process fixed strings.  Use the StringBuffer class to process flexible strings.  Use the StringTokenizer.
Computer Programming 2 Lecture 5: String Processing Part b: Class StringTokenizer Prepared & Presented by: Mahmoud Rafeek Alfarra MINISTRY OF EDUCATION.
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.
1 StringTokenization Overview l StringTokenizer class l Some StringTokenizer methods l StringTokenizer examples.
Objectives ► Become familiar with the class String ► ► Learn how to use input and output dialog boxes in a program ► ► Learn how to tokenize the input.
Strings Chapter 7 CSCI CSCI 1302 – Strings2 String Comparisons Compare string contents with the equals(String s) method not == String s0 = “ Java”;
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.
Strings and Text File I/O (and Exception Handling) Corresponds with Chapters 8 and 17.
1 The String Class F Constructing a String: F Obtaining String length and Retrieving Individual Characters in a string F String Concatenation (concat)
Garside, JAVA: First Contact, 2ed Java First Contact – 2 nd Edition Garside and Mariani Chapter 6 More Java Data Types.
1 CHAPTER 3 StringTokenizer. 2 StringTokenizer CLASS There are BufferedReader methods to read a line (i.e. a record) and a character, but not just a single.
Files and Streams CS /02/05 L7: Files Slide 2 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved.
String Object String is a sequence of characters. Unlike many other programming languages that implements string as character arrays, Java implements strings.
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.
CS 115 OBJECT ORIENTED PROGRAMMING I LECTURE 9 GEORGE KOUTSOGIANNAKIS Copyright: 2014 Illinois Institute of Technology- George Koutsogiannakis 1.
Strings and Related Classes String and character processing Class java.lang.String Class java.lang.StringBuffer Class java.lang.Character Class java.util.StringTokenizer.
17-Feb-16 String and StringBuilder Part I: String.
© 2006 Pearson Addison-Wesley. All rights reserved 1-1 Chapter 1 Review of Java Fundamentals.
String & Exception. Lesson plan Class & Object String Exercise for midterm.
Object Oriented Programming Lecture 2: BallWorld.
String and StringBuffer classes
Object-Oriented Java Programming
Strings.
EKT 472: Object Oriented Programming
String and String Buffers
String class in java Visit for more Learning Resources string.
String and StringBuilder
Chapter 7: Strings and Characters
MSIS 655 Advanced Business Applications Programming
Unit-2 Objects and Classes
String and StringBuilder
String and StringBuilder
Java – String Handling.
Lecture 07 String Jaeki Song.
OBJECT ORIENTED PROGRAMMING I LECTURE 9 GEORGE KOUTSOGIANNAKIS
String and StringBuilder
Object Oriented Programming
Chapter 7 Strings Processing strings using the String class, the StringBuffer class, and the StringTokenizer class. Use the String class to process fixed.
Object-Oriented Java Programming
JAVA – String Function PROF. S. LAKSHMANAN,
In Java, strings are objects that belong to class java.lang.String .
Presentation transcript:

1 String Buffer & String Tokenizer Overview l Overview of String Buffer class and Methods l Overview of String Tokenizer class and methods l Preview: Notions of Object Orientation

2 Overview of String Buffer l java.lang.StringBuffer is a public class, extending the object class, and contains methods for creating, parsing and modifying string buffers. l Strings can not be changed once created, whereas the StringBuffer Class allows changes dynamically l Important StringBuffer class Methods: public StringBuffer(); public StringBuffer(int); public StringBuffer(String); public int length(); public int capacity(); public synchronized char charAt(int); public synchronized void setCharAt(int, char); public synchronized StringBuffer append(Object); public synchronized StringBuffer insert(int, Object); public synchronized StringBuffer insert(int, char[]); public synchronized StringBuffer reverse(); public String toString();

3 Using String Buffer class Example 1 class InsertTest { public static void main(String[] args) { StringBuffer sb = new StringBuffer("Drink Java!"); sb.insert(6, "Hot "); System.out.println(sb.toString()); } Example 2 class ReverseString { public static String reverseIt(String source) { int i, len = source.length(); StringBuffer dest = new StringBuffer(len); for (i = (len - 1); i >= 0; i--) { dest.append(source.charAt(i)); } return dest.toString(); } public static void main(String[] args) { String str = "What's going on?"; System.out.println(ReverseString.reverseIt(str)) ; }

4 StringTokenizer Class l The StringTokenizer class is another built-in class that is very useful in string processing. l It allows a string to be broken into substrings, called tokens, depending on a given set of characters, called delimiters. Signatures: public StringTokenizer(String str) public StringTokenizer(String str, String delim) public StringTokenizer(String str, String delim, boolean returnTokens) An instance of StringTokenizer behaves in two ways, depending on whether it was created with returnTokens flag as true or false. »If flag is false, delimiters are simply used as separators »If flag is true, delimiters themselves are returned as tokens l Basic Methods: public boolean hasMoreTokens() public String nextToken() public String nextToken(String delim) public int countTokens()

5 Using StringTokenizer Class l The following example shows how String Tokenizer may be used. Example 1: import java.util.*; class StringTokenizerTest { public static void main (String [] args) { String s = "Platform independence, the Java compiler, and the Java interpreter"; StringTokenizer st = new StringTokenizer(s, ",", false); while (st.hasMoreTokens()) { System.out.println(st.nextToken()); } st = new StringTokenizer(s); System.out.println("Number of tokens with blank as a token seperator = " + st.countTokens()); } Output: Platform independence the Java compiler and the Java interpreter Number of tokens with blank as a token seperator = 9

6 Using StringTokenizer Class (Cont’d) l The following program takes a sequence of integer values as a string, breaks the string into substrings of individual integers, converts each to integer and adds and prints the sum. Example 2: import java.util.*; class TokenizeIntegerString { public static void main(String[] args) { String str = new String(" "); StringTokenizer st = new StringTokenizer(str); int sum=0; while (st.hasMoreTokens()) { sum+=Integer.parseInt(st.nextToken()); } System.out.println("For the string:\"“ +str+ "\""); System.out.println("Sum of the integers is: "+sum); } Output: For the string: " " Sum of the integers is: 130