String Handling StringBuffer class character class StringTokenizer class.

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.
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.
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.
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.
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.
©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.
Chapter 9: Text Processing and More about Wrapper Classes Starting Out with Java: From Control Structures through Objects Fifth Edition by Tony Gaddis.
Java How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 7 Strings.
String Class. Objectives and Goals Identify 2 types of Strings: Literal & Symbolic. Learn about String constructors and commonly used methods Learn several.
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.
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”;
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.
Strings Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006 by Maria Litvin, Gary Litvin, and.
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,
String Object String is a sequence of characters. Unlike many other programming languages that implements string as character arrays, Java implements strings.
G51PR1 Introduction to Programming I University of Nottingham Unit 8 : Strings.
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.
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,
17-Feb-16 String and StringBuilder Part I: String.
© 2006 Pearson Addison-Wesley. All rights reserved 1-1 Chapter 1 Review of Java Fundamentals.
Chapter 10: Text Processing and More about Wrapper Classes
The String class is defined in the java.lang package
String and StringBuffer classes
Strings and Text Processing
Strings, Characters and Regular Expressions
Strings, StringBuilder, and Character
Strings.
EKT 472: Object Oriented Programming
University of Central Florida COP 3330 Object Oriented Programming
String and String Buffers
String class in java Visit for more Learning Resources string.
Modern Programming Tools And Techniques-I Lecture 11: String Handling
String and StringBuilder
Chapter 7: Strings and Characters
Object Oriented Programming
MSIS 655 Advanced Business Applications Programming
String and StringBuilder
String and StringBuilder
String Handling String, StringBuffer, StringBuilder
Java – String Handling.
Lecture 07 String Jaeki Song.
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.
In Java, strings are objects that belong to class java.lang.String .
Presentation transcript:

String Handling StringBuffer class character class StringTokenizer class

StringBuffer class Provides much of the functionalities of Strings Represents growable and writable character sequences May have the charcters in the middle or appended to the end Will grow automatically Allow room for growth

StringBuffer Constructors StringBuffer() Creates with no parameters Reserves room for 16 characters without reallocation StringBuffer(int size) Explicitly sets the size of the buffer

Constructor StringBuffer(String str) Str sets the initial contents of the StringBuffer Reserves room for 16 more characters object without reallocation Reallocation is expensive process in terms of time Frequent reallocation can fragment memory

Methods length() Give number of characters or length of a StringBuffer int length() capacity() Total allocated capacity can be found int capacity()

ensureCapacity() capacity specifies the size of the buffer To preallocate room for a certain number of characters after a stringBuffer has been constructed To set the size of the buffer Useful if you know in advance that you will be appending a large number of small strings to a stringBuffer void ensureCapacity(int capacity)

setLength() To set the length of the buffer within a StringBuffer object If we increase the size of the buffer, null characters are added to the end of the existing buffer If we setLength() with a value less than the current value returned by length(), then the characters stored beyond the new length will be lost void setLength(int len)

charAt() Extraction of single character from a stringBuffer char charAt(int where) setCharAt() Set a specified character at the specified position void charAt(int where, char ch) getChars() To copy a substring of a StringBuffer into an array void getChars(int sourceStart, int sourceEnd, char target[], int targetStart)

append() To concatenate the string representation of any other type of data to the end of the invoking StringBuffer object StringBuffer append(String str) StringBuffer append(int num) StringBuffer append(Object obj) String.valueOf() is called for each parameter to obtain its string representation The result is appended to the current StringBuffer object

insert() Inserts one string into another It calls String.valueOf() to obtain the string representation of the value it is called with This string is then inserted into the invoking StringBuffer object StringBuffer insert(int index, String str) StringBuffer insert(int index, char ch) StringBuffer insert(int index, Object obj)

reverse() Can reverse the characters within a StringBuffer object Return the reversed object on which it is performed StringBuffer reverse() replace() Replaces one set of characters with another set inside a StringBuffer StringBuffer replace(int startIndex, int endIndex, String str)

delete() To delete characters from the StringBuffer object StringBuffer delete(int startIndex, int endIndex) Deletes the sequence of characters from the invoking object StringBuffer delete(int loc) Deletes a single character at that specified location Returns a StringBuffer object

substring() Returns the specified segment of the StringBuffer String substring(int startIndex) Returns the string from startIndex to the ned of the invoking StringBuffer object String substring(int startIndex, int endIndex) Returns the substring that starts at startIndex and runs thru end-1

Character class The type-wrapped class for character Most of the methods are static and take atleast a character argument Also contains a constructor that receives a char argument to initialize a character object Character(char ch) ch – specifies the character that will be wrapped by the Character object

char charValue() To obtain the char value contained in character object Returns the character static boolean isDefined(char ch) Returns true if ch is defined by unicode. static boolean isDigit(char ch) Returns true if ch is a digit.

static boolean isJavaIdentifierStart(char ch) Returns true if ch is allowed as the first character of the Java identifier static boolean isJavaIdentifierPart(char ch) Returns true if ch is allowed as the part character of the Java identifier(other than the first character) static boolean isLetter(char ch) Returns true if ch is a letter. static boolean isLetterOrDigit(char ch) Returns true if ch is a digit or a letter.

static boolean isLowerCase(char ch) Returns true if ch is a lower case letter static boolean isUpperCase(char ch) Returns true if ch is a upper case letter static boolean isSpaceCase(char ch) Returns true if ch is a space character static char toLowerCase(char ch) Returns lowercase equivalent of ch static char toUpperCase(char ch) Returns uppercase equivalent of ch

static char forDigit(int digit, int radix) Returns a char after converting the integer digit into a character in the number system specified by the integer radix(base of the number) static int digit(char ch, int radix) Returns the integer value after converting the character ch into an integer in the number system specified by the integer radix(base of the number) static boolean equals(Charater ch) Returns true if the contents in object ch is equals to contents in the invoked object

StringTokenizer class Is available in java.util package That breaks a string into its component tokens Tokens are seperated from one another using delimiters Default delimiters – blank, tab, new line, carriage return Other characters used as delimiters : ;,

Constructors StringTokenizer(String str) str is the string that will be tokenized the default delimiters are used StringTokenizer(String str, String delimiters) Delimiters is a string that specifies the delimiters StringTokenizer(String str, String delimiters, boolean delimAsToken) If delimAsToken true, then the delemiters are also returned as tokens when the string is parsed

Methods int countTokens() Using the current set of delimiters, the method determines the number of tokens left to be parsed and returns the result String nextToken() Returns the next token as a string boolean hasMoreTokens() Returns true if one or tokens remain in the string and returns false if there is none