String Handling(String Class)
String Handling(String Class)
String Handling
Example:
System.out.println("This is a String, too");
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)
Examples
Example:
int age = 9;
String s = "He is " + age + " years old.";
System.out.println(s);
Methods of String class
1.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());
2. 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);
Methods Cont…
4. toCharArray(): returns a character array initialized by the
contents of the string.
public char [] toCharArray();
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.
class Teststringcomparison3
{
public static void main(String args[])
{
String s1="Sachin";
String s2="Sachin";
String s3=new String("Sachin");
System.out.println(s1==s2);
//true (because both refer to same instance)
System.out.println(s1==s3);
//
false(because s3 refers to instance created in nonpool
)
}
}
3. String compare by compareTo() method
The String compareTo() method compares values lexicographically and
returns an integer value that describes if first string is less than, equal
to or greater than second string.
Suppose s1 and s2 are two string variables. If:
s1 == s2 :0
s1 > s2 :positive value
s1 < s2 :negative value
class Teststringcomparison4
{
public static void main(String args[])
{
String s1="Sachin";
String s2="Sachin";
String s3="Ratan";
System.out.println(s1.compareTo(s2));//0
System.out.println(s1.compareTo(s3));//1(because s1>s3)
System.out.println(s3.compareTo(s1));//-1(because s3 < s1 )
}
}
String Comparison
startsWith( ) and endsWith( ):
String s="Sachin";
System.out.println(s.startsWith("Sa"));
//true
System.out.println(s.endsWith("n"));
//true
String Comparison
compareTo( ):
For indexOf( ), the search runs from startIndex to the end of the
string.
String replaceString=s1.replace("Java","Kav
a");
//
replaces all occurrences of "Java" to "Kava"
System.out.println(replaceString);
trim( )
The trim( ) method returns a copy of the invoking string from which
any leading and trailing whitespace has been removed.
String trim( )
Example:
String s = " Hello World ".trim();
This puts the string “Hello World” into s.
The string trim() method eliminates white spaces before and after
string.
Example
String s=" Sachin ";
System.out.println(s);// Sachin
System.out.println(s.trim());//Sachin
Changing the Case of Characters Within a String
String toLowerCase( )
String toUpperCase( )
Example
String s="Sachin";
System.out.println(s.toUpperCase());
//SACHIN
System.out.println(s.toLowerCase());
//sachin
System.out.println(s);
//Sachin(no change in original)