String HandlingString Class
String HandlingString Class
execute
public Basic1()
{
System.out.println("Basic1 constructor");
}
public Basic1(int a)
{
{
System.out.println("Welcome to LPU");
}
public Basic1()
{
System.out.println("Basic1 constructor");
}
public Basic1(int a)
{
String Handling
Example:
System.out.println("This is a String, too");
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Why String is Immutable or Final?
String has been widely used as parameter for many java classes e.g.
for opening network connection we can pass hostname and port
number as string ,
we can pass database URL as string for opening database
connection,
we can open any file in Java by passing name of file as argument to
File I/O classes.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Introduction
In java, four predefined classes are provided that either
represent strings or provide functionality to manipulate them.
Those classes are:
◦ String
◦ StringBuffer
◦ StringBuilder
◦ StringTokenizer
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Important
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Why String Handling?
String handling is required to perform following operations
on some string:
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Creating String objects
class StringDemo
{
public static void main(String args[])
{
String strOb1 = “Ravi";
String strOb2 = “LPU";
String strOb3 = strOb1 + " and " + strOb2;
System.out.println(strOb1);
System.out.println(strOb2);
System.out.println(strOb3);
}
}
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
String Class
String Constructor:
public String ()
public String (String)
public String (char [])
public String (byte [])
public String (char [], int offset, int no_of_chars)
public String (byte [], int offset, int no_of _bytes)
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Examples
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
String Concatenation
Concatenating Strings:
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
String Concatenation with Other Data Types
We can concatenate strings with other types of data.
Example:
int age = 9;
String s = "He is " + age + " years old.";
System.out.println(s);
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Methods of String class
String Length:
length() returns the length of the string i.e. number of
characters.
Example:
char chars[] = { 'a', 'b', 'c' };
String s = new String(chars);
System.out.println(s.length());
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
concat( ): used to concatenate two strings.
String concat(String str)
This method creates a new object that contains the invoking string with
the contents of str appended to the end.
Example:
String s1 = "one"; String s2 = s1.concat("two");
Example:
char ch;
ch = "abc".charAt(1);
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Methods Cont…
toCharArray(): returns a character array initialized by the
contents of the string.
public char [] toCharArray();
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Methods Cont…
getChars(): used to obtain set of characters from the string.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
String Comparison
equals(): used to compare two strings for equality.
Comparison is case-sensitive.
Note:
This method is defined in Object class and overridden in String class.
equals(), in Object class, compares the value of reference not the content.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Example
class equalsDemo {
public static void main(String args[]) {
String s1 = "Hello";
String s2 = "Hello";
String s3 = "Good-bye";
String s4 = "HELLO";
System.out.println(s1 + " equals " + s2 + " -> " +
s1.equals(s2));
System.out.println(s1 + " equals " + s3 + " -> " +
s1.equals(s3));
System.out.println(s1 + " equals " + s4 + " -> " +
s1.equals(s4));
System.out.println(s1 + " equalsIgnoreCase " + s4 + " ->
“ +s1.equalsIgnoreCase(s4));
}
}
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
String Comparison
startsWith( ) and endsWith( ):
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
String Comparison
compareTo( ):
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Example
class SortString {
static String arr[] = {"Now", "is", "the", "time", "for", "all", "good", "men",
"to", "come", "to", "the", "aid", "of", "their", "country"};
public static void main(String args[]) {
for(int j = 0; j < arr.length; j++) {
for(int i = j + 1; i < arr.length; i++) {
if(arr[i].compareTo(arr[j]) < 0) {
String t = arr[j];
arr[j] = arr[i];
arr[i] = t;
}
}
System.out.println(arr[j]);
}
}
}
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Searching Strings
The String class provides two methods that allow us to search a
string for a specified character or substring:
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
We can specify a starting point for the search using these forms:
For indexOf( ), the search runs from startIndex to the end of the
string.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Example
class indexOfDemo {
public static void main(String args[]) {
String s = "Now is the time for all good men " +
"to come to the aid of their country.";
System.out.println(s);
System.out.println("indexOf(t) = " + s.indexOf('t'));
System.out.println("lastIndexOf(t) = " + s.lastIndexOf('t'));
System.out.println("indexOf(the) = " + s.indexOf("the"));
System.out.println("lastIndexOf(the) = " + s.lastIndexOf("the"));
System.out.println("indexOf(t, 10) = " + s.indexOf('t', 10));
System.out.println("lastIndexOf(t, 60) = " + s.lastIndexOf('t', 60));
System.out.println("indexOf(the, 10) = " + s.indexOf("the", 10));
System.out.println("lastIndexOf(the, 60) = " + s.lastIndexOf("the",
60));
}
}
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Modifying a String
Because String objects are immutable, whenever we want to modify a
String, it will construct a new copy of the string with modifications.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
replace( ): The replace( ) method has two forms.
The first replaces all occurrences of one character in the invoking
string with another character. It has the following general form:
String toLowerCase( )
String toUpperCase( )
Example:
String str = "have-fun-in-java@blogspot@in";
String [] s = str.split("-", -2);
for(String x: s) System.out.println(x);
limit > 0 : If this is the case then the pattern will be applied at
most limit-1 times, the resulting array’s length will not be
more than n, and the resulting array’s last entry will contain
all input beyond the last matched pattern.
limit < 0 : In this case, the pattern will be applied as many times
as possible, and the resulting array can be of any size.
Example:
String joinString1=String.join(“-”, “have",“fun",“in“, “java”);
System.out.println(joinString1);