Introduction to Java
Using String Class (Contd.)
Some of the most commonly used methods in the String class
are:
int length()
char charAt(int index)
void getChars(int srcBegin,int srcEnd, char[] dst,
int dstBegin)
boolean equals(object obj)
int compareTo(String str)
boolean startsWith(String prefix)
boolean endsWith(String suffix)
int indexOf(int ch)
int indexOf(int ch)
int lastIndexOf(int ch)
String subString(int beginindex)
String concat(String str)
Ver 1.0 Slide 1 of 11
Introduction to Java
Using String Class (Contd.)
String replace(char oldChar,char newChar)
String toUpperCase()
String toLowerCase()
String trim()
char[] toCharArray()
String valueOf(Object obj)
boolean equalsIgnoreCase(String anotherString)
Ver 1.0 Slide 2 of 11
Introduction to Java
Using StringBuilder and StringBuffer Classes
StringBuilder and StringBuffer classes are used to work
with strings.
These classes are mutable classes as they do not create any new
string object when manipulated.
Therefore, when you need to do various manipulations, such as
appending, concatenating, and deleting with string literals, you
should use StringBuilder and StringBuffer.
The following code snippet initializes a string object to a
StringBuilder reference:
StringBuilder s1= new StringBuilder(“Hello”);
Ver 1.0 Slide 3 of 11
Introduction to Java
Using StringBuilder and StringBuffer Classes (Contd.)
Some of the most commonly used methods in the
StringBuilder class are:
StringBuilder append(String obj)
StringBuilder delete(int start, int end)
StringBuilder insert(int offset, String obj)
StringBuilder reverse()
Ver 1.0 Slide 4 of 11
Introduction to Java
Just a minute
Which one of the following String class methods is used to copy
characters from a source string object into the destination character
array?
charAt()
getChars()
toCharArray()
substring()
Ver 1.0 Slide 5 of 11
Introduction to Java
Just a minute (Contd.)
Solution:
getChars()
Ver 1.0 Slide 6 of 11
Introduction to Java
Activity: Manipulating Arrays and Strings
Problem Statement:
In the Hangman game, when a user decides to play the Hangman
game, the numbers of dashes or blanks that are equal to the length of
a word are displayed. To implement this, Peter needs to store a group
of words, randomly pick a word, calculate the length of the word, and
finally displays the number of dashes or blanks. In addition, he wants to
implement the functionality that takes an alphabetical character as an
input from the user, check if the character entered by the user exists in
the corresponding word, and display the letter in the appropriate
dashes. However, if the character doesn’t exist in the word, it is added
to the list of missed letters. Help Peter to achieve the preceding
requirement.
Ver 1.0 Slide 7 of 11
Introduction to Java
Activity: Manipulating Arrays and Strings (Contd.)
Solution: To perform the activity, refer the steps given in the
embedded document.
Microsoft Word
Document
Ver 1.0 Slide 8 of 11
Introduction to Java
Summary
In this session, you learned that:
An array is a collection of elements of a single data type stored in
adjacent memory locations.
You can access an array element by specifying the name and the
subscript number of the array.
The subscript number specifies the position of an element within the
array. It is also called the index of the element.
The various types of array are one-dimensional array and
multidimensional array.
A one-dimensional array is a collection of elements with a single index
value.
A one-dimensional array can have multiple columns but only one row.
Multidimensional arrays are arrays of arrays.
Ver 1.0 Slide 9 of 11
Introduction to Java
Summary (Contd.)
A multidimensional array can have multiple columns and rows.
To display all the elements stored in the array, you can use the for
loop.
To know the total number of elements in the array, you can use the
length property.
Java provides the for-each loop to iterate through an array.
In Java, a list of predefined values can be created using enum.
An enum is a special type of a class, which can have constructors,
methods, and instance variables.
An enum can be declared either inside the class or outside the class.
In Java, enums are similar to classes.
To access enum constants, you can either use an enum name or enum
reference.
Ver 1.0 Slide 10 of 11
Introduction to Java
Summary (Contd.)
To store string literals, you can use the String class in the java.lang
package.
In Java, String classes are immutable classes. This means that once
a string object is created, you cannot change its value.
Every time you manipulate a string object, a new string object is
created in the memory.
StringBuilder and StringBuffer classes are mutable classes as
they do not create any new string object when manipulated.
Ver 1.0 Slide 11 of 11