+ All Categories
Home > Engineering > Learn Java Part 7

Learn Java Part 7

Date post: 08-Aug-2015
Category:
Upload: gurpreet-singh
View: 359 times
Download: 2 times
Share this document with a friend
Popular Tags:
20
StringBuffer and its functions, StringTokenizer, its functions and its working
Transcript
Page 1: Learn Java Part 7

StringBuffer and its functions, StringTokenizer, its functions and its working

Page 2: Learn Java Part 7

A string buffer is like a String, but can be modified. At any point in time it contains someparticular sequence of characters, but the length and content of the sequence can bechanged through certain method calls.

Creating object:

StringBuffer sb=new StringBuffer(“Hello”);orString s=“Hello”;StringBuffer sb=new StringBuffer(s);

Page 3: Learn Java Part 7

StringBuffer append(Any Data Type)- Appends the specified CharSequence to this sequence

StringBuffer s=new StringBuffer(“Hello”);s.append(“ Java”); //s will be updatedSystem.out.print(s);will print Hello Java

System.out.print(s.append(123));will print Hello123

s=s.append(5.69); //s will be updatedSystem.out.print(s);will print Hello5.69

Page 4: Learn Java Part 7

• int length()- returns the length of stringStringBuffer s=new StringBuffer(“Hello”);int len=s.length();System.out.print(len);will print 5

• char charAt(int index) – returns the character at given indexStringBuffer s1=new StringBuffer(“ABCDEFG”);System.out.print(s1.charAt(3));will print D

• int indexOf(String str)- returns the first index from where given string starts

int indexOf(String str,int fromIndex)- returns the first index from where given string

starts after the fromIndex. fromIndex - the index from which to start the search

StringBuffer s1=new StringBuffer(“ABCDEFABCDEF”);System.out.print(s1. indexOf(“BCD”));will print 1System.out.print(s1. indexOf(“B”,5));will print 7

Page 5: Learn Java Part 7

• int lastIndexOf (String str)- returns the last index from where given string starts

int lastIndexOf(String str,int last)- returns the last index from where given string starts

before the last index. last is included

StringBuffer s1=new StringBuffer(“ABCDEFABCDEF”);

System.out.print(s1. lastIndexOf(“BCD”));will print 7

• StringBuffer reverse()- Causes this character sequence to be replaced by the reverse ofthe sequence.

StringBuffer sb=new StringBuffer(“ABCDEFG”);sb.reverse();System.out.print(sb);will print GFEDCBA

A B C D E F A B C D E F

0 1 2 3 4 5 6 7 8 9 10 11

Page 6: Learn Java Part 7

• Boolean equals(Object obj)- Indicates whether some other object obj is "equal to" thisone

StringBuffer sb=new StringBuffer("Hello");StringBuffer s=new StringBuffer("Hello");

if(s.equals(sb))System.out.println("True");

elseSystem.out.println("false");

will print false because sb and s contains different references or you can say that both sband s points to different objects(has different address).

StringBuffer sb=new StringBuffer("Hello");StringBuffer s=sb;

if(s.equals(sb))System.out.println("True");

elseSystem.out.println("false");

will print true because sb and s contains same references or you can say that both sb and spoints to same objects(has same address).

Page 7: Learn Java Part 7

• String substring(int start)- Returns a new String that contains a subsequence ofcharacters currently contained in this character sequence. The substring begins at thespecified index and extends to the end of this sequence. ‘start’ is included.

StringBuffer s1=new StringBuffer(“ABCDEF”);System.out.print(s1. substring(3));will print DEF

• String substring(int start,int end)- returns substring from ‘start’ index to ‘end’ index.‘start’ is included while ‘end’ is excluded

StringBuffer s1=new StringBuffer(“ABCDEF”);System.out.print(s1. substring(2,5));will print CDE

A B C D E F

0 1 2 3 4 5

Page 8: Learn Java Part 7

• StringBuffer replace(int start, int end, String str)- Replaces the characters in a

substring of this sequence with characters in the specified String.start - The beginning index, inclusive.end - The ending index, exclusive.str - String that will replace previous contents

StringBuffer s1=new StringBuffer(“ABCDEF”);s1.replace(0,2,”Hello ”);System.out.print(s1);will print Hello CDEF

• StringBuffer delete(int start,int end)- delete the string from index start to end-1StringBuffer s1=new StringBuffer(“ABCDEF”);s1.delete(0,3);System.out.print(s1);will print DEF

A B C D E F

0 1 2 3 4 5

Page 9: Learn Java Part 7

• StringBuffer insert(int start, Any data Type)- insert the given type at index start andshifts forward the remaining characters

start - The beginning indexStringBuffer s1=new StringBuffer(“ABCDEF”);s1.insert(3,”Hello ”);System.out.print(s1);will print ABCHelloDEF

• StringBuffer deleteCharAt(int index)- deletes the character at given indexStringBuffer s1=new StringBuffer(“ABCDEF”);s1.deleteCharAt(3);System.out.print(s1);will print ABCEF

A B C D E F

0 1 2 3 4 5

Page 10: Learn Java Part 7

• StringBuffer setCharAt(int index, char ch)- set character at index to given character chStringBuffer s1=new StringBuffer(“ABCDEF”);s1.setCharAt(3,’#’);System.out.print(s1);will print ABC#EF

• String toString()- converts the given StringBuffer to StringStringBuffer s1=new StringBuffer(“ABCDEF”);String s=s1.toString();System.out.print(s);will print ABCDEF

Now to convert the given String to StringBufffer you can use:

String s=“hello”;StringBuffer sb=new StringBuffer(s);orStringBuffer sb=new StringBuffer(“Hello”);

A B C D E F

0 1 2 3 4 5

Page 11: Learn Java Part 7

The string tokenizer class allows an application to break a string into tokens.Creating object:

StringTokenizer st=new StringTokenizer(“Hello this is GsbProgramming”);orString s=“Hello this is GsbProgramming”;StringTokenizer st=new StringTokenizer(s);

The default delimiter(after which a new token will be started) is SPACEso, st has following tokens:1. Hello2. this3. is4. GsbProgramming

You can specify your own delimiters also. For example:StringTokenizer st=new StringTokenizer(“Hello,this is,GsbProgramming”, “,”);Now delimiter will be comma(,)

Page 12: Learn Java Part 7

Now st has following tokens:1. Hello2. this is3. GsbProgramming

StringTokenizer st=new StringTokenizer(“Hello,this is,GsbProgramming”, “”);will have only one token:1. Hello,this is,Gsbprogramming

Suppose if you want that delimiter will also be a token then you can make object like:StringTokenizer st=new StringTokenizer(“Hello,this is,GsbProgramming”, “,”,true);now st will have following tokens:1. Hello2. ,3. this is4. ,5. GsbProgrammingif you specify true then delimiters will also be counted as token, if you specify falsedelimiters will not be taken as a token

Page 13: Learn Java Part 7

You can also specify more than one delimiters as:StringTokenizer st=new StringTokenizer(“Hello,this is,GsbProgramming”, “, ”);delimiters are comma(,) and spaceHere st will have following tokens:1. Hello2. this3. is4. Gsbprogramming

StringTokenizer st=new StringTokenizer(“Hello,this is,GsbProgramming”, “, ”,true);now st will have following tokens:1. Hello2. ,3. this4. (space)5. is6. ,7. GsbProgramming

Page 14: Learn Java Part 7

• int countTokens()- returns the total tokens left at given time.StringTokenizer st=new StringTokenizer(“Hello this is GsbProgramming”);System.out.println(“Total Tokens: ”+st.countTokens());will print Total Tokens: 4

• String nextToken()- returns the next token, and advances the pointerStringTokenizer st=new StringTokenizer(“Hello this is GsbProgramming”);System.out.println(“First Token: ”+st.nextToken());will print: First Token: Hello

• boolean hasMoreToken()- returns true if any token is left for processing otherwisereturns false

StringTokenizer st=new StringTokenizer(“Hello this is GsbProgramming”);if(st.hasMoreToken()){

System.out.print(“Token is Available”);}will print: Token is Available

Page 15: Learn Java Part 7

StringTokenizer maintains a cursor or pointer that points to current token.For example:StringTokenizer st=new StringTokenizer(“Hello this is GsbProgramming”);when we write this statement it will convert the string “Hello this is Gsbprogramming” intotokens and place the pointer at first token

Tokens:

Current position of pointer

So when we write st.countTokens() it will return 4 because total tokens after thepointer(including token at current position ) are 4Therefore st.hasMoreToken() will return true

Token 1 Token 2 Token 3 Token 4

Hello this is GsbProgramming

Page 16: Learn Java Part 7

now if we write st.nextToken() it will return the token at current position of pointer, i.e. itwill return Hello and advances the position of pointer as below:

Current position of pointer

So when we write st.countTokens() it will return 3 because total tokens after thepointer(including token at current position ) are 3Therefore st.hasMoreToken() will return true

Token 1 Token 2 Token 3 Token 4

Hello this is GsbProgramming

Page 17: Learn Java Part 7

now if we again write st.nextToken() it will return the token at current position of pointer,i.e. it will return this and advances the position of pointer as below:

Current position of pointer

So when we write st.countTokens() it will return 2 because total tokens after thepointer(including token at current position ) are 2Therefore st.hasMoreToken() will return true

Token 1 Token 2 Token 3 Token 4

Hello this is GsbProgramming

Page 18: Learn Java Part 7

now if we again write st.nextToken() it will return the token at current position of pointer,i.e. it will return is and advances the position of pointer as below:

Current position of pointer

So when we write st.countTokens() it will return 1 because total tokens after thepointer(including token at current position ) are 1Therefore st.hasMoreToken() will return true

Token 1 Token 2 Token 3 Token 4

Hello this is GsbProgramming

Page 19: Learn Java Part 7

now if we again write st.nextToken() it will return the token at current position of pointer,i.e. it will return GsbProgramming and deletes pointer as below:

So when we write st.countTokens() it will return 0 because there is no tokens after thepointer(including token at current position )Therefore st.hasMoreToken() will return false

Now if we write st.nextToken() it will generate an exception: NoSuchElementException

Token 1 Token 2 Token 3 Token 4

Hello this is GsbProgramming

Page 20: Learn Java Part 7

For more Visit:http://gsb-programming.blogspot.in/search/label/Java


Recommended