SlideShare a Scribd company logo
StringBuffer and its functions, StringTokenizer, its functions and its working
A string buffer is like a String, but can be modified. At any point in time it contains some
particular sequence of characters, but the length and content of the sequence can be
changed through certain method calls.
Creating object:
StringBuffer sb=new StringBuffer(“Hello”);
or
String s=“Hello”;
StringBuffer sb=new StringBuffer(s);
StringBuffer append(Any Data Type)- Appends the specified CharSequence to this sequence
StringBuffer s=new StringBuffer(“Hello”);
s.append(“ Java”); //s will be updated
System.out.print(s);
will print Hello Java
System.out.print(s.append(123));
will print Hello123
s=s.append(5.69); //s will be updated
System.out.print(s);
will print Hello5.69
• int length()- returns the length of string
StringBuffer s=new StringBuffer(“Hello”);
int len=s.length();
System.out.print(len);
will print 5
• char charAt(int index) – returns the character at given index
StringBuffer 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 1
System.out.print(s1. indexOf(“B”,5));
will print 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 of
the 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
• Boolean equals(Object obj)- Indicates whether some other object obj is "equal to" this
one
StringBuffer sb=new StringBuffer("Hello");
StringBuffer s=new StringBuffer("Hello");
if(s.equals(sb))
System.out.println("True");
else
System.out.println("false");
will print false because sb and s contains different references or you can say that both sb
and s points to different objects(has different address).
StringBuffer sb=new StringBuffer("Hello");
StringBuffer s=sb;
if(s.equals(sb))
System.out.println("True");
else
System.out.println("false");
will print true because sb and s contains same references or you can say that both sb and s
points to same objects(has same address).
• String substring(int start)- Returns a new String that contains a subsequence of
characters currently contained in this character sequence. The substring begins at the
specified 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
• 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-1
StringBuffer 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
• StringBuffer insert(int start, Any data Type)- insert the given type at index start and
shifts forward the remaining characters
start - The beginning index
StringBuffer s1=new StringBuffer(“ABCDEF”);
s1.insert(3,”Hello ”);
System.out.print(s1);
will print ABCHelloDEF
• StringBuffer deleteCharAt(int index)- deletes the character at given index
StringBuffer 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
• StringBuffer setCharAt(int index, char ch)- set character at index to given character ch
StringBuffer s1=new StringBuffer(“ABCDEF”);
s1.setCharAt(3,’#’);
System.out.print(s1);
will print ABC#EF
• String toString()- converts the given StringBuffer to String
StringBuffer 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);
or
StringBuffer sb=new StringBuffer(“Hello”);
A B C D E F
0 1 2 3 4 5
The string tokenizer class allows an application to break a string into tokens.
Creating object:
StringTokenizer st=new StringTokenizer(“Hello this is GsbProgramming”);
or
String s=“Hello this is GsbProgramming”;
StringTokenizer st=new StringTokenizer(s);
The default delimiter(after which a new token will be started) is SPACE
so, st has following tokens:
1. Hello
2. this
3. is
4. GsbProgramming
You can specify your own delimiters also. For example:
StringTokenizer st=new StringTokenizer(“Hello,this is,GsbProgramming”, “,”);
Now delimiter will be comma(,)
Now st has following tokens:
1. Hello
2. this is
3. 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. Hello
2. ,
3. this is
4. ,
5. GsbProgramming
if you specify true then delimiters will also be counted as token, if you specify false
delimiters will not be taken as a token
You can also specify more than one delimiters as:
StringTokenizer st=new StringTokenizer(“Hello,this is,GsbProgramming”, “, ”);
delimiters are comma(,) and space
Here st will have following tokens:
1. Hello
2. this
3. is
4. Gsbprogramming
StringTokenizer st=new StringTokenizer(“Hello,this is,GsbProgramming”, “, ”,true);
now st will have following tokens:
1. Hello
2. ,
3. this
4. (space)
5. is
6. ,
7. GsbProgramming
• 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 pointer
StringTokenizer 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 otherwise
returns false
StringTokenizer st=new StringTokenizer(“Hello this is GsbProgramming”);
if(st.hasMoreToken())
{
System.out.print(“Token is Available”);
}
will print: Token is Available
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” into
tokens 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 the
pointer(including token at current position ) are 4
Therefore st.hasMoreToken() will return true
Token 1 Token 2 Token 3 Token 4
Hello this is GsbProgramming
now if we write st.nextToken() it will return the token at current position of pointer, i.e. it
will 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 the
pointer(including token at current position ) are 3
Therefore st.hasMoreToken() will return true
Token 1 Token 2 Token 3 Token 4
Hello this is GsbProgramming
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 the
pointer(including token at current position ) are 2
Therefore st.hasMoreToken() will return true
Token 1 Token 2 Token 3 Token 4
Hello this is GsbProgramming
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 the
pointer(including token at current position ) are 1
Therefore st.hasMoreToken() will return true
Token 1 Token 2 Token 3 Token 4
Hello this is GsbProgramming
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 the
pointer(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
For more Visit:
http://gsb-programming.blogspot.in/search/label/Java

More Related Content

What's hot (20)

PDF
Python Regular Expressions
BMS Institute of Technology and Management
 
PPTX
String (Computer programming and utilization)
Digvijaysinh Gohil
 
PPT
Pythonintroduction
-jyothish kumar sirigidi
 
DOCX
Unitii string
Sowri Rajan
 
PDF
Pratt Parser in Python
Percolate
 
PPT
strings
teach4uin
 
PDF
Introduction to python
Marian Marinov
 
PPT
Python
Vishal Sancheti
 
PPTX
Strings in Python
Amisha Narsingani
 
PDF
Functions in python
Ilian Iliev
 
PDF
Fundamentals of Cryptography - Caesar Cipher - Python
Isham Rashik
 
PPT
Unit vii wp ppt
Bhavsingh Maloth
 
PPTX
String Methods and Files
Pooja B S
 
PPTX
Iteration
Pooja B S
 
PPT
14 strings
Rohit Shrivastava
 
PPTX
String in programming language in c or c++
Azeemaj101
 
PDF
5 2. string processing
웅식 전
 
Python Regular Expressions
BMS Institute of Technology and Management
 
String (Computer programming and utilization)
Digvijaysinh Gohil
 
Pythonintroduction
-jyothish kumar sirigidi
 
Unitii string
Sowri Rajan
 
Pratt Parser in Python
Percolate
 
strings
teach4uin
 
Introduction to python
Marian Marinov
 
Strings in Python
Amisha Narsingani
 
Functions in python
Ilian Iliev
 
Fundamentals of Cryptography - Caesar Cipher - Python
Isham Rashik
 
Unit vii wp ppt
Bhavsingh Maloth
 
String Methods and Files
Pooja B S
 
Iteration
Pooja B S
 
14 strings
Rohit Shrivastava
 
String in programming language in c or c++
Azeemaj101
 
5 2. string processing
웅식 전
 

Viewers also liked (14)

PDF
Learn Java Part 9
Gurpreet singh
 
PDF
Stars
Gurpreet singh
 
PDF
Learn Java Part 11
Gurpreet singh
 
PDF
Learn Java Part 8
Gurpreet singh
 
PDF
Learn Java Part 5
Gurpreet singh
 
PDF
Learn Java Part 4
Gurpreet singh
 
PDF
Defing locations in Oracle Apps
Gurpreet singh
 
PDF
Learn Java Part 10
Gurpreet singh
 
PDF
Assigning role AME_BUS_ANALYST
Gurpreet singh
 
PDF
Creating business group in oracle apps
Gurpreet singh
 
PDF
Learn Java Part 1
Gurpreet singh
 
PDF
Learn Java Part 2
Gurpreet singh
 
PPT
Chapter3 basic java data types and declarations
sshhzap
 
PPTX
Introduction to Jquery
Gurpreet singh
 
Learn Java Part 9
Gurpreet singh
 
Learn Java Part 11
Gurpreet singh
 
Learn Java Part 8
Gurpreet singh
 
Learn Java Part 5
Gurpreet singh
 
Learn Java Part 4
Gurpreet singh
 
Defing locations in Oracle Apps
Gurpreet singh
 
Learn Java Part 10
Gurpreet singh
 
Assigning role AME_BUS_ANALYST
Gurpreet singh
 
Creating business group in oracle apps
Gurpreet singh
 
Learn Java Part 1
Gurpreet singh
 
Learn Java Part 2
Gurpreet singh
 
Chapter3 basic java data types and declarations
sshhzap
 
Introduction to Jquery
Gurpreet singh
 
Ad

Similar to Learn Java Part 7 (20)

PPTX
Java Strings
RaBiya Chaudhry
 
PPTX
Strings in Java
Abhilash Nair
 
PPTX
STRING CLASS AND STRING BUFFER CLASS CONCEPTS IN JAVA
pkavithascs
 
PPT
Strings
naslin prestilda
 
PPT
Strings power point in detail with examples
rabiyanaseer1
 
PPTX
Java String
SATYAM SHRIVASTAV
 
PDF
Module-1 Strings Handling.ppt.pdf
learnEnglish51
 
PPTX
L14 string handling(string buffer class)
teach4uin
 
PPTX
L13 string handling(string class)
teach4uin
 
PPT
Strings In OOP(Object oriented programming)
Danial Virk
 
PPTX
Java string handling
GaneshKumarKanthiah
 
PPT
Charcater and Strings.ppt Charcater and Strings.ppt
mulualem37
 
PPTX
String.pptx
RanjithKumar742256
 
PPTX
StringBuffer.pptx
meenakshi pareek
 
PPT
07slide
Aboudi Sabbah
 
PPTX
Day_5.1.pptx
ishasharma835109
 
PPT
Strings.ppt
SanthiyaAK
 
PPT
String and string manipulation
Shahjahan Samoon
 
PDF
String.ppt
ajeela mushtaq
 
PPTX
String Handling, Inheritance, Packages and Interfaces
Prabu U
 
Java Strings
RaBiya Chaudhry
 
Strings in Java
Abhilash Nair
 
STRING CLASS AND STRING BUFFER CLASS CONCEPTS IN JAVA
pkavithascs
 
Strings power point in detail with examples
rabiyanaseer1
 
Java String
SATYAM SHRIVASTAV
 
Module-1 Strings Handling.ppt.pdf
learnEnglish51
 
L14 string handling(string buffer class)
teach4uin
 
L13 string handling(string class)
teach4uin
 
Strings In OOP(Object oriented programming)
Danial Virk
 
Java string handling
GaneshKumarKanthiah
 
Charcater and Strings.ppt Charcater and Strings.ppt
mulualem37
 
String.pptx
RanjithKumar742256
 
StringBuffer.pptx
meenakshi pareek
 
07slide
Aboudi Sabbah
 
Day_5.1.pptx
ishasharma835109
 
Strings.ppt
SanthiyaAK
 
String and string manipulation
Shahjahan Samoon
 
String.ppt
ajeela mushtaq
 
String Handling, Inheritance, Packages and Interfaces
Prabu U
 
Ad

More from Gurpreet singh (19)

PDF
Oracle Fusion REST APIs with Get Invoice API example
Gurpreet singh
 
PDF
PL/SQL for Beginners - PL/SQL Tutorial 1
Gurpreet singh
 
PDF
Creating ESS Jobs for Oracle Fusion BIP Reports
Gurpreet singh
 
PDF
Introduction to Oracle Fusion BIP Reporting
Gurpreet singh
 
PDF
Why Messaging system?
Gurpreet singh
 
PDF
Understanding Flex Fields with Accounting Flexfields(Chart of Accounts) in O...
Gurpreet singh
 
PPTX
Oracle Application Developmenr Framework
Gurpreet singh
 
PDF
Java Servlet part 3
Gurpreet singh
 
PDF
Oracle advanced queuing
Gurpreet singh
 
PDF
Oracle SQL Part 3
Gurpreet singh
 
PDF
Oracle SQL Part 2
Gurpreet singh
 
PDF
Oracle SQL Part1
Gurpreet singh
 
PDF
Generics and collections in Java
Gurpreet singh
 
PDF
IO Streams, Serialization, de-serialization, autoboxing
Gurpreet singh
 
PDF
Java Servlets Part 2
Gurpreet singh
 
PDF
Introduction to Data Flow Diagram (DFD)
Gurpreet singh
 
PDF
Ingenium test(Exam Management System) Project Presentation (Full)
Gurpreet singh
 
PDF
Computer Graphics Notes
Gurpreet singh
 
PDF
Learn Java Part 11
Gurpreet singh
 
Oracle Fusion REST APIs with Get Invoice API example
Gurpreet singh
 
PL/SQL for Beginners - PL/SQL Tutorial 1
Gurpreet singh
 
Creating ESS Jobs for Oracle Fusion BIP Reports
Gurpreet singh
 
Introduction to Oracle Fusion BIP Reporting
Gurpreet singh
 
Why Messaging system?
Gurpreet singh
 
Understanding Flex Fields with Accounting Flexfields(Chart of Accounts) in O...
Gurpreet singh
 
Oracle Application Developmenr Framework
Gurpreet singh
 
Java Servlet part 3
Gurpreet singh
 
Oracle advanced queuing
Gurpreet singh
 
Oracle SQL Part 3
Gurpreet singh
 
Oracle SQL Part 2
Gurpreet singh
 
Oracle SQL Part1
Gurpreet singh
 
Generics and collections in Java
Gurpreet singh
 
IO Streams, Serialization, de-serialization, autoboxing
Gurpreet singh
 
Java Servlets Part 2
Gurpreet singh
 
Introduction to Data Flow Diagram (DFD)
Gurpreet singh
 
Ingenium test(Exam Management System) Project Presentation (Full)
Gurpreet singh
 
Computer Graphics Notes
Gurpreet singh
 
Learn Java Part 11
Gurpreet singh
 

Recently uploaded (20)

PPTX
Day2 B2 Best.pptx
helenjenefa1
 
PPTX
Heart Bleed Bug - A case study (Course: Cryptography and Network Security)
Adri Jovin
 
PDF
monopile foundation seminar topic for civil engineering students
Ahina5
 
PDF
Pressure Measurement training for engineers and Technicians
AIESOLUTIONS
 
PPTX
MPMC_Module-2 xxxxxxxxxxxxxxxxxxxxx.pptx
ShivanshVaidya5
 
PPTX
Types of Bearing_Specifications_PPT.pptx
PranjulAgrahariAkash
 
PPTX
The Role of Information Technology in Environmental Protectio....pptx
nallamillisriram
 
PPTX
Benefits_^0_Challigi😙🏡💐8fenges[1].pptx
akghostmaker
 
PPTX
Hashing Introduction , hash functions and techniques
sailajam21
 
PPTX
Break Statement in Programming with 6 Real Examples
manojpoojary2004
 
PPTX
265587293-NFPA 101 Life safety code-PPT-1.pptx
chandermwason
 
PDF
Water Design_Manual_2005. KENYA FOR WASTER SUPPLY AND SEWERAGE
DancanNgutuku
 
PPTX
Solar Thermal Energy System Seminar.pptx
Gpc Purapuza
 
PDF
Statistical Data Analysis Using SPSS Software
shrikrishna kesharwani
 
PPTX
Green Building & Energy Conservation ppt
Sagar Sarangi
 
PPTX
Thermal runway and thermal stability.pptx
godow93766
 
PDF
Biomechanics of Gait: Engineering Solutions for Rehabilitation (www.kiu.ac.ug)
publication11
 
PPTX
ISO/IEC JTC 1/WG 9 (MAR) Convenor Report
Kurata Takeshi
 
PPTX
Innowell Capability B0425 - Commercial Buildings.pptx
regobertroza
 
PDF
MAD Unit - 2 Activity and Fragment Management in Android (Diploma IT)
JappanMavani
 
Day2 B2 Best.pptx
helenjenefa1
 
Heart Bleed Bug - A case study (Course: Cryptography and Network Security)
Adri Jovin
 
monopile foundation seminar topic for civil engineering students
Ahina5
 
Pressure Measurement training for engineers and Technicians
AIESOLUTIONS
 
MPMC_Module-2 xxxxxxxxxxxxxxxxxxxxx.pptx
ShivanshVaidya5
 
Types of Bearing_Specifications_PPT.pptx
PranjulAgrahariAkash
 
The Role of Information Technology in Environmental Protectio....pptx
nallamillisriram
 
Benefits_^0_Challigi😙🏡💐8fenges[1].pptx
akghostmaker
 
Hashing Introduction , hash functions and techniques
sailajam21
 
Break Statement in Programming with 6 Real Examples
manojpoojary2004
 
265587293-NFPA 101 Life safety code-PPT-1.pptx
chandermwason
 
Water Design_Manual_2005. KENYA FOR WASTER SUPPLY AND SEWERAGE
DancanNgutuku
 
Solar Thermal Energy System Seminar.pptx
Gpc Purapuza
 
Statistical Data Analysis Using SPSS Software
shrikrishna kesharwani
 
Green Building & Energy Conservation ppt
Sagar Sarangi
 
Thermal runway and thermal stability.pptx
godow93766
 
Biomechanics of Gait: Engineering Solutions for Rehabilitation (www.kiu.ac.ug)
publication11
 
ISO/IEC JTC 1/WG 9 (MAR) Convenor Report
Kurata Takeshi
 
Innowell Capability B0425 - Commercial Buildings.pptx
regobertroza
 
MAD Unit - 2 Activity and Fragment Management in Android (Diploma IT)
JappanMavani
 

Learn Java Part 7

  • 1. StringBuffer and its functions, StringTokenizer, its functions and its working
  • 2. A string buffer is like a String, but can be modified. At any point in time it contains some particular sequence of characters, but the length and content of the sequence can be changed through certain method calls. Creating object: StringBuffer sb=new StringBuffer(“Hello”); or String s=“Hello”; StringBuffer sb=new StringBuffer(s);
  • 3. StringBuffer append(Any Data Type)- Appends the specified CharSequence to this sequence StringBuffer s=new StringBuffer(“Hello”); s.append(“ Java”); //s will be updated System.out.print(s); will print Hello Java System.out.print(s.append(123)); will print Hello123 s=s.append(5.69); //s will be updated System.out.print(s); will print Hello5.69
  • 4. • int length()- returns the length of string StringBuffer s=new StringBuffer(“Hello”); int len=s.length(); System.out.print(len); will print 5 • char charAt(int index) – returns the character at given index StringBuffer 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 1 System.out.print(s1. indexOf(“B”,5)); will print 7
  • 5. • 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 of the 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
  • 6. • Boolean equals(Object obj)- Indicates whether some other object obj is "equal to" this one StringBuffer sb=new StringBuffer("Hello"); StringBuffer s=new StringBuffer("Hello"); if(s.equals(sb)) System.out.println("True"); else System.out.println("false"); will print false because sb and s contains different references or you can say that both sb and s points to different objects(has different address). StringBuffer sb=new StringBuffer("Hello"); StringBuffer s=sb; if(s.equals(sb)) System.out.println("True"); else System.out.println("false"); will print true because sb and s contains same references or you can say that both sb and s points to same objects(has same address).
  • 7. • String substring(int start)- Returns a new String that contains a subsequence of characters currently contained in this character sequence. The substring begins at the specified 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
  • 8. • 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-1 StringBuffer 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
  • 9. • StringBuffer insert(int start, Any data Type)- insert the given type at index start and shifts forward the remaining characters start - The beginning index StringBuffer s1=new StringBuffer(“ABCDEF”); s1.insert(3,”Hello ”); System.out.print(s1); will print ABCHelloDEF • StringBuffer deleteCharAt(int index)- deletes the character at given index StringBuffer 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
  • 10. • StringBuffer setCharAt(int index, char ch)- set character at index to given character ch StringBuffer s1=new StringBuffer(“ABCDEF”); s1.setCharAt(3,’#’); System.out.print(s1); will print ABC#EF • String toString()- converts the given StringBuffer to String StringBuffer 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); or StringBuffer sb=new StringBuffer(“Hello”); A B C D E F 0 1 2 3 4 5
  • 11. The string tokenizer class allows an application to break a string into tokens. Creating object: StringTokenizer st=new StringTokenizer(“Hello this is GsbProgramming”); or String s=“Hello this is GsbProgramming”; StringTokenizer st=new StringTokenizer(s); The default delimiter(after which a new token will be started) is SPACE so, st has following tokens: 1. Hello 2. this 3. is 4. GsbProgramming You can specify your own delimiters also. For example: StringTokenizer st=new StringTokenizer(“Hello,this is,GsbProgramming”, “,”); Now delimiter will be comma(,)
  • 12. Now st has following tokens: 1. Hello 2. this is 3. 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. Hello 2. , 3. this is 4. , 5. GsbProgramming if you specify true then delimiters will also be counted as token, if you specify false delimiters will not be taken as a token
  • 13. You can also specify more than one delimiters as: StringTokenizer st=new StringTokenizer(“Hello,this is,GsbProgramming”, “, ”); delimiters are comma(,) and space Here st will have following tokens: 1. Hello 2. this 3. is 4. Gsbprogramming StringTokenizer st=new StringTokenizer(“Hello,this is,GsbProgramming”, “, ”,true); now st will have following tokens: 1. Hello 2. , 3. this 4. (space) 5. is 6. , 7. GsbProgramming
  • 14. • 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 pointer StringTokenizer 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 otherwise returns false StringTokenizer st=new StringTokenizer(“Hello this is GsbProgramming”); if(st.hasMoreToken()) { System.out.print(“Token is Available”); } will print: Token is Available
  • 15. 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” into tokens 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 the pointer(including token at current position ) are 4 Therefore st.hasMoreToken() will return true Token 1 Token 2 Token 3 Token 4 Hello this is GsbProgramming
  • 16. now if we write st.nextToken() it will return the token at current position of pointer, i.e. it will 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 the pointer(including token at current position ) are 3 Therefore st.hasMoreToken() will return true Token 1 Token 2 Token 3 Token 4 Hello this is GsbProgramming
  • 17. 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 the pointer(including token at current position ) are 2 Therefore st.hasMoreToken() will return true Token 1 Token 2 Token 3 Token 4 Hello this is GsbProgramming
  • 18. 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 the pointer(including token at current position ) are 1 Therefore st.hasMoreToken() will return true Token 1 Token 2 Token 3 Token 4 Hello this is GsbProgramming
  • 19. 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 the pointer(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