= 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)) ; }"> = 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)) ; }">

Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 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 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 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 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 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 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("1 23 34 72"); 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: "1 23 34 72" Sum of the integers is: 130


Download ppt "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."

Similar presentations


Ads by Google