STRING HANDLING
OOPS USING JAVA
Contents
• String
• StringBuffer
• StringTokenizer
String Class
• String is probably the most commonly
used class in Java's class library.
• The first thing to understand about strings
is that every string you create is actually
an object of type String.
• For example, in the statement
String str = new
String("example");
String str = "example";
String Class
• Java defines one operator for String
objects: +.
• It is used to concatenate two strings.
For example, this statement
• String myString = "I" + " like " +
"Java.";
results in myString contains
"I like Java."
String Class Methods
METHOD DESCRIPTION
Returns the length of this
int length( )
string.
Returns the char value at the
char charAt (int index) specified index. An index
ranges from 0 to length() - 1.
Returns the index within this
int indexOf (int ch) string of the first occurrence of
the specified character.
Returns the index within this
int lastIndexOf (int ch) string of the last occurrence of
the specified character.
Returns a new string that is a
String substring (int bi, int ei) substring of this string
String Class Methods
METHOD DESCRIPTION
String toUpperCase ( ) Converts all of the characters
in this String to upper case.
String toLowerCase ( ) Converts all of the characters
in this String to lower case.
String concat(String str) Concatenates the specified
string to the end of this string.
Compares two strings
int compareTo(String s2) lexicographically.
Compares this string to the
boolean equals(Object ob) specified object.
String Buffer
• As we know, String Class represents the
fixed length, immutable strings.
• In Java StringBuffer class is used to create
mutable (modifiable) string.
• The principal operations on
StringBuffer are
the append and insert methods
StringBuffer Constructors
StringBuffer()
StringBuffer(String str)
StringBuffer(int capacity)
• The first form creates an empty string buffer with the initial
capacity of 16.
• The second form creates a string buffer with the specified
string.
• The third form creates an empty string buffer with the
specified capacity as length.
StringBuffer sb = new StringBuffer();
[Link]("Hello");
[Link](" ");
[Link]("world");
StringBuffer Methods
METHOD DESCRIPTION
int length( ) Returns the length.
int capacity( ) Returns the current capacity.
Returns the char value in this
sequence at the specified
char charAt(int index)
index.
The character at the specified
void setCharAt(int index,
index is set to ch.
char ch)
Causes this character
StringBuffer reverse ( ) sequence to be replaced by
the reverse of the sequence.
StringBuffer Methods
METHOD DESCRIPTION
Appends the string
representation of the type
StringBuffer append(Type ob) argument “ob” to the
sequence.
Inserts the string
representation of
StringBuffer insert (int offset,
Type ob)
the Type argument “ob” into
this sequence.
Removes the characters in a
StringBuffer delete(int start,
int end)
substring of this sequence.
Replaces the characters in a
substring of this sequence with
StringBuffer replace(int st,
int end, String str)
characters in the
specified String.
String Tokenizer
• The string tokenizer class allows an
application to break the given string
into tokens.
• StringTokenizer class is avilable
[Link] package.
StringTokenizer
Constructors
StringTokenizer (String str)
Constructs a string tokenizer for the specified
string.
The tokenizer uses the default delimiter , which
is the white space character.
StringTokenizer (String str, String delim)
Constructs a string tokenizer for the specified
string and delimiter.
The characters in the delim argument are the
delimiters for separating tokens
StringTokenizer Methods
int countTokens()
Returns the total number of tokens.
boolean hasMoreTokens()
Returns true if one or more tokens remain
in the string and returns false if there are
none.
String nextToken( )
Returns the next token as a String.