1 StringBuffer & StringTokenizer Classes Chapter 5 - Other String Classes.

Slides:



Advertisements
Similar presentations
1 Streams and Input/Output Files Part 2. 2 Files and Exceptions When creating files and performing I/O operations on them, the systems generates errors.
Advertisements

1 Arrays, Strings and Collections [2] Rajkumar Buyya Grid Computing and Distributed Systems (GRIDS) Laboratory Dept. of Computer Science and Software Engineering.
1 Streams and Input/Output Files Part 3. 2 Handling Primitive Data Types The basic input and output streams provide read/write methods that can be used.
The ArrayList Class and the enum Keyword
Purpose : To convert this string to a new character array. Return Type : char[ ] Parameters : none Declaration : public char[ ] toCharArray() Returns.
Lecture 15: I/O and Parsing
String Pemrograman Berbasis Obyek Oleh Tita Karlita.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 9 Strings.
Formal Language, chapter 4, slide 1Copyright © 2007 by Adam Webber Chapter Four: DFA Applications.
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 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.
Chapter 7 Strings F Processing strings using the String class, the StringBuffer class, and the StringTokenizer class. F Use the String class to process.
Java Programming Strings Chapter 7.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 9 Strings and Text I/O.
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.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 9 Characters and Strings (sections ,
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.
COMP 14 Introduction to Programming Miguel A. Otaduy May 17, 2004.
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.
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 ,
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.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
Java Utility Classes CS 21b. Some Java Utility Classes Vector Hashtable StringTokenizer * import java.util.*;
Session 05 Java Strings and Files. Exercise Complete the “quick-and-dirty” class CharacterCounter containing only a main() method that displays the number.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 7 Strings.
Lecture 14 March 23, Exam Results Class Average was 69.4 –Median was 72.5 (which means there were some very low grades) Questions 31, 32, and 33.
Chapter 7 Strings  Use the String class to process fixed strings.  Use the StringBuffer class to process flexible strings.  Use the StringTokenizer.
Chapter 7 Strings F Processing strings using the String class, the StringBuffer class, and the StringTokenizer class. F Use the String class to process.
1 StringTokenization Overview l StringTokenizer class l Some StringTokenizer methods l StringTokenizer examples.
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.
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.
Lecture 5 I/O and Parsing
Chapter 5 Programming with Objects and Classes OO Programming Concepts OO Programming Concepts Declaring and Creating Objects Declaring and Creating Objects.
Strings And other things. Strings Overview The String Class and its methods The char data type and Character class StringBuilder, StringTokenizer classes.
Strings and Related Classes String and character processing Class java.lang.String Class java.lang.StringBuffer Class java.lang.Character Class java.util.StringTokenizer.
Java I--Copyright © 2000 Tom Hunter. Strings and Characters.
© 2006 Pearson Addison-Wesley. All rights reserved 1-1 Chapter 1 Review of Java Fundamentals.
Strings Chapter 7 CSCI CSCI 1302 – Strings2 Outline Introduction The String class –Constructing a String –Immutable and Canonical Strings –String.
Object-Oriented Java Programming
Strings, Characters and Regular Expressions
Strings, StringBuilder, and Character
Strings.
The StringBuffer Class 
EKT 472: Object Oriented Programming
Chapter 7: Strings and Characters
MSIS 655 Advanced Business Applications Programming
Unit-2 Objects and Classes
Lecture 07 String Jaeki Song.
Chapter 10 Thinking in Objects Part 2
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
In Java, strings are objects that belong to class java.lang.String .
Presentation transcript:

1 StringBuffer & StringTokenizer Classes Chapter 5 - Other String Classes

2 StringBuffer Class StringBuffer is an alternative to the String class In general it can be used anywhere a String can be used StringBuffer is more flexible than the String class When to use: –When the string is not changing use String class –When the string is changing or being dynamically built use StringBuffer

3 StringBuffer Constructors Constructs a string buffer with no characters and an initial capacity of 16 StringBuffer strBuffer = new StringBuffer( ); Constructs a string buffer with no characters and an initial capacity specified by the argument StringBuffer strBuffer = new StringBuffer( 20 ); Constructs a string buffer with the same character sequence as the string argument - initial capacity is 16 plus the length of the argument StringBuffer strBuffer = new StringBuffer( s );

4 StringBuffer Methods int capacity( ) returns the integer value of the current capacity of the string buffer StringBuffer reverse( ) returns the reverse of the character sequence of the string contained in the string buffer int length( ) returns the number of characters in the string buffer

5 StringBuffer Methods void setLength( int newLength ) sets the length of the string buffer and will possibily truncate the string buffer, additional capacity is null char charAt( int index ) returns the character at the specified index void setCharAt( int index, char ch ) sets the character at the specified index to the specified character

6 StringBuffer Methods StringBuffer append( ) appends the string representation of the argument to the string buffer StringBuffer insert( ) inserts the string representation of the argument into the string buffer

7 StringBuffer Example StringBuffer strBuf = new StringBuffer( ); strBuf.append( Welcome ); strBuf.append( ); strBuf.append( to ); strBuf.append( ); strBuf.append( Java ); strBuf.insert( 11, HTML and );

8 StringBuffer Notes Every string buffer has a capacity If capacity is exceeded, it is automatically made larger to accommodate additional characters When the string changes frequently, the string buffer is more efficient

9 StringTokenizer Class Used to break a string into its individual pieces Each piece is an individual word within the string Uses a set of characters as delimiters to distinguish the breaks between words Each individual word is called a token

10 StringTokenizer Constructors Constructor for a string with specific delimiters with delimiter returned with the token public StringTokenizer( String s, String delim, boolean return ) Constructor for a string with a specific delimiters and the delimiter is not returned with token public StringTokenizer( String s, String delim ) Constructor for a string with default delimiters \t\n\r and not returned with token public StringTokenizer( String s )

11 StringTokenizer Methods boolean hasMoreTokens( ) returns true if any tokens are left in the string String nextToken( ) returns the next token in the string String nextToken( String delim ) returns the next token after resetting delimiter int countTokens( ) returns the number of tokens remaining in the string

12 StringTokenizer Usage import java.util.StringTokenizer; static BufferedReader keyboard = new BufferedReader( new InputStreamReader(System.in) ); StringTokenizer st = new StringTokenizer( keyboard.readLine() ); int age = new Integer( st.nextToken( ) ).intValue;