Module 5
Module 5
ENUMERATIONS
ENUMERATION FUNDAMENTALS
• Ex
}
}
Modifying a String
• substring( )
– You can extract a substring using substring( ). It
has two forms.
– The first is String substring(int startIndex)
– The second form of substring( ) allows you to
specify both the beginning and ending index of
the substring:
• String substring(int startIndex, int endIndex)
Changing the Case of Characters Within a String
– The method toLowerCase( ) converts all the characters in a string from
uppercase to lowercase.
– The toUpperCase( ) method converts all the characters in a string from
lowercase to uppercase
– General forms of these methods:
• String toLowerCase( ) String toUpperCase( )
Example:
class ChangeCase {
public static void main(String args[])
{
String s = "Lord Krishna";
System.out.println("Original: " + s);
String upper = s.toUpperCase();
String lower = s.toLowerCase();
System.out.println("Uppercase: " + upper);
System.out.println("Lowercase: " + lower);
Output:
• Origianl: Lord Krishna
• Uppercase: LORD KRISHNA
• Lowercase: lord krishna
StringBuffer
StringBuffer may have characters and substrings
inserted in the middle or appended to the end.
StringBuffer Constructors
– StringBuffer defines these four constructors:
– StringBuffer( ): The default constructor (the one with no
parameters) reserves room for 16 characters without
reallocation.
– StringBuffer(int size): accepts an integer argument that
explicitly sets the size of the buffer.
– StringBuffer(String str): accepts a String argument that
sets the initial contents of the StringBuffer object and
reserves room for 16 more characters without
reallocation
– StringBuffer(CharSequence chars): creates an object that
contains the character sequence contained in chars.
• length( )
– The current length of a StringBuffer can be found
via the length( ) method.
– They have the following general forms:
• int length( ) int capacity( )
class StringBufferDemo {
public static void main(String args[])
{
StringBuffer sb = new StringBuffer("Krishna");
System.out.println("buffer = " + sb);
// displays Krishna
System.out.println("length = " + sb.length());
// displays 7
}
}
• setLength( )
– To set the length of the buffer within a
StringBuffer object, use setLength( ). Its general
form is shown here:
• void setLength(int len)
charAt( ) and setCharAt( )
– The value of a single character can be obtained from a
StringBuffer via the charAt( ) method.
• You can set the value of a character within a StringBuffer
using setCharAt().
• Their general forms are shown here: char charAt(int
where)
• void setCharAt(int where, char ch)
Example:
• StringBuffer sb = new StringBuffer("Krishna");
sb.charAt(2)// selects i
• sb.setCharAt(2, “u‘)// sets second index value to u means
krushna
insert( )
– The insert( ) method inserts one string into another. It is overloaded to accept
values of all the simple types, plus Strings, Objects, and CharSequences.
These are a few of its forms:
• StringBuffer insert(int index, String str)
Example:
class insertDemo {