• iconJava Online Training In Andhra Pradesh and Telangana
  • icon9010519704

Opening Hours :7AM to 9PM

String Handling in Java

Java String Handling

String Handling

Performing various operation on String value is known as String handling.In java language String can be handled with the help of following predefined classes.
1. String class
2. StringBuffer class
3. StringBuilder class

String Class:

It is also predefiend class in java Language in java.lang package. Whose object is immutable( Value can not be changed )

Syntax:
String s="Value"; // Datatype
or
String objref=new String("Value"); // Object 
Example:
String as datatype:
String s1="Sateesh";

String as class
String s2=new String("Sateesh");

String class offers following methods to perform various operations on String value.

String Methods:

1.String length()
2.String charAt()
3.String compareTo()
4.String compareToIgnoreCase()
5.String concat()
6.String startsWith()
7.String endsWith()
8.String equals()
9.String equalsIgnoreCase()
10.String indexOf()
11.String lastIndexOf()
12.String substring()
13.String toLowerCase()
14.String toUpperCase()
15.String trim()


StringBuffer Methods

1.String insert()
2.String delete()
3.String append()
4.String replace()


StringBuilder Methods

Both String and StringBuffer Methods

MSK TechnologiesTrainings







String length():

Which can be used to find number of characters of given string.

Syntax:
String objref=new String("Value");
objref.length();


public class Main
{
    public static void main(String args[])
    {
        String s=new String("Sateesh");
        System.out.println(s.length());
    }
}                          

Output:
7
String charAt():

Which can be used to find the cheracter at the given index value.

Syntax:
String objref=new String("Value");
objref.charAt(indexvalue);


public class Main
{
    public static void main(String args[])
    {
        String s=new String("Sateesh");
        System.out.println(s.charAt(2));
    }
}

Output:
t
String compareTo()

Which can be used to compare two strings.This method returns an integer value . It returns zero both the strings same otherwise it returns non zero integer value.

Syntax:
String objref1=new String("Value");
String objref2=new String("Value");
int n=objref1.compareTo(objref2);


public class main
{
    public static void main(String args[])
    {
        String s1=new String("sateesh");
        String s2=new String("SATEESH");
        int n=s1.compareTo(s2);
        if(n==0)
        {
            System.out.println("Both Are Equal");
        }
        else
        {
            System.out.println("Both Are Not Equal");
        }
    }
}
                               

Output:
Both Are Not Equal
String compareToIgnoreCase()

Which also similar to compareTo method.But it is case sensitive method. that means both the strings are having same value with different case.That the JVM treats both are same strings.

Syntax:
String objref1=new String("Value");
String objref2=new String("Value");
int n=objref1.compareToIgnoreCase(objref2);


public class Main
{
    public static void main(String args[])
    {
        String s1=new String("sateesh");
        String s2=new String("SATEESH");
        int n=s1.compareToIgnoreCase(s2);
        if(n==0)
        {
            System.out.println("Both Are Equal");
        }
        else
        {
            System.out.println("Both Are Not Equal");
        }
    }
}                           

Output:
Both Are Equal
String concat()

Which can be used to combine two strings into a single string.

Syntax:
String objref1=new String("Value");
String objref2=new String("Value");
objref1.concat(objref2);


public class Main
{
    public static void main(String args[])
    {
        String s1=new String("Sateesh");
        String s2=new String("kumar");
        System.out.println(s1.concat(s2));
    }
}
                               

Output:
Sateeshkumar
String startsWith()

Which returns true if the string is starts with given substring otherwise return false

Syntax:
String objref1=new String("Value");
objref1.startsWith("substring");


public class Main
{
    public static void main(String args[])
    {
        String s1=new String("Welcome To Java Class");
        System.out.println(s1.startsWith("Welcome"));
    }
}                             

Output:
true
String endsWith()

Which returns true if the string is ends with given substring otherwise return false

Syntax:
String objref1=new String("Value");
objref1.endsWith("substring");

public class Main
{
    public static void main(String args[])
{ String s1=new String("Welcome To Java Class"); System.out.println(s1.endsWith("Welcome")); } }

Output:
false
String equals()

Which can be used to compare two strings(content).Which returns true both the strings are same otherwise returns false.

Syntax:
String objref1=new String("Value");
String objref2=new String("Value");
objref1.equals(objref2);
Note:
It is case sensitive method.


public class Main
{
    public static void main(String args[])
    {
        String s1=new String("sateesh");
        String s2=new String("SATEESH");
        if(s1.equals(s2))
        {
            System.out.println("Both Are Equal");
        }
        else
        {
            System.out.println("Both Are Not Equal");
        }
    }
}
                              

Output:
Both Are Not Equal
String equalsIgnoreCase()

Which can be used to compare two strings(content).Which returns true both the strings are same otherwise returns false.But here ignore case.

Syntax:
String objref1=new String("Value");
String objref2=new String("Value");
objref1.equalsIgnoreCase(objref2);


public class Main
{
    public static void main(String args[])
    {
        String s1=new String("sateesh");
        String s2=new String("SATEESH");
        if(s1.equalsIgnoreCase(s2))
        {
            System.out.println("Both Are Equal");
        }
        else
        {
            System.out.println("Both Are Not Equal");
        }
    }
}

Output:
Both Are Equal

Note: what is difference between equals() and == ?
equals always comes the content whereas == compare the reference;
Excample:
String s1="Hello";
String s2=new String("Hello");
s1.equals(s2); -- true
s1==s2; -- false (references of both the strings different)
String indexOf()

Which can be used to get the index value from the given string or character(first occurence)

Syntax:
String objref=new String("Value");
objref.indexOf('character');

objref.indexOf("String");

public class Main
{
    public static void main(String args[])
    {
        String s1=new String("sateesh");
        System.out.println(s1.indexOf('e'));
    }
}
                             

Output:
3
String lastIndexOf()

Which can be used to get the index value from the given string or character(last occurence)

Synrax :
String objref=new String("Value");
objref.lastIndexOf('character');
objref.lastIndexOf("String");


public class Main
{
    public static void main(String args[])
    {
        String s1=new String("sateesh");
        System.out.println(s1.lastIndexOf('e'));
    }
}                         

Output:
4
String substring()

Which can be used to get the substring or part of a string from the given string.
1. substring(index): usedto get the substring starting with the index value to last character.

Example:
String objref=new String("Value");
 objref1.substring(index);

2. substring("starting index,ending index): it can be used to get the specific value from given string.

Example:
String objref=new String("Value");
objref.substring(starting index,ending index);


public class Main
{
    public static void main(String args[])
    {
        String s1=new String("sateesh");
        System.out.println(s1.substring(2));
    }
}


Output: teesh


public class Main
{
    public static void main(String args[])
    {
        String s1=new String("sateesh");
        System.out.println(s1.substring(2,5));
    }
}


Output: tee
String toLowerCase()

Which can be used to convert upper case to lower case

Syntax:
String objref=new String("Value");
objref.tolowerCase();


public class Main
{
    public static void main(String args[])
    {
        String s=new String("SATEESH");
        System.out.println(s.toLowerCase());
    }
}
                          

Output: sateeshm
String toUpperCase()
Which can be used to convert lower case to upper case
Syntax:
String objref=new String("Value");
objref.toUpperCase();

public class Main
{
    public static void main(String args[])
    {
        String s=new String("sateesh");
        System.out.println(s.toUpperCase());
    }
}                           

Output: SATEESH
String trim()

It is used to trim the starting and ending spaces from the given strings

Syntax:
String objref=new String("Value");
objref.trim();


public class Main
{
    public static void main(String args[])
    {
        String s1=new String("     Sateesh     ");
        System.out.println("Before Trim String length "+s1.length());
        
        String s2=s1.trim();
        System.out.println(s2);
        System.out.println("After Trim String length "+s1.length());
    }
}
                             

Output: Before Trim String length 9
Sateesh
After Trim String length 7
Sateesh

String Buffer Class

it is a predefined class in java.lang package.which can be used to perform various operations on string .But whose object is mutable( content of the StringBuffer can be changed)

Syntax:
StringBuffer objref=new StringBuffer("Value");
String insert()

which can be used to insert either a new characer or string at given index value.

Syntax:
objref.insert(index,'char');
objref.insert(index,"String");


public class Main
{
    public static void main(String args[])
    {
        StringBuffer s1=new StringBuffer("sateesh  ");
        System.out.println(s1.insert(7,'M'));
    }
}


Output: sateeshM


public class Main
{
    public static void main(String args[])
    {
        StringBuffer s1=new StringBuffer("sateesh  ");
        System.out.println(s1.insert(7,"kumar"));
    }
}

Output:sateesh kumar
String delete()

Which can be used to delete either character or string from the main string.
1. deleteCharAt()
2. delete()

Syntax:
                                   
 objref.deleteCharAt(index);  //Delete One Character
objref.delete(start index,ending index); //Delete Multiple Characters


public class Main
{
    public static void main(String args[])
    {
        StringBuffer s1=new StringBuffer("sateesh  ");
        System.out.println(s1.deleteCharAt(1));
    }
}
 

Output: steesh
    
public class Main
{
    public static void main(String args[])
    {
        StringBuffer s1=new StringBuffer("sateesh  ");
        System.out.println(s1.delete(1,3));
    }
}

Output: seesh
String append()

Which can be used to add a new String at the end of existing string.

Syntax:
    
 objref.append("String value");
 


public class Main
{
    public static void main(String args[])
    {
        StringBuffer s1=new StringBuffer("sateesh  ");
        System.out.println(s1.append("kumar"));
    }
}
                           

Output: sateesh kumar
String replace()

Which can be used to replace a new String in to old string based on the index value.

Syntax:
    
 objref.replace(starting index,ending index,"new String")

public class Main
{
    public static void main(String args[])
    {
        StringBuffer s1=new StringBuffer("sateesh  ");
        System.out.println(s1.replace(1,7,"kumar"));
    }
}                             

Output: skumar
String Builder Class

It is a same as StringBuffer class but only difference is StringBuffer is synchronized where as StringBuilder is non synchronized.


Syntax:
    
StringBuilder objref=new StringBuilder("Value");

StringBuilder supports all the methods of String and StringBuffer.


MSK TechnologiesTrainings

String Practice Programs
1. Write a String length program
2. Write a string charAt() method program
3. Write a string toLowerCase() Program
4. Write a String toUpperCase() program
5. Write a string concat() program
6. Write a string compareTo program
7. Write a string compareToIgnoreCase() Program
8. Write a string equals program
9. Write a string equalsIgnoreCase() program
10. Write a string startsWith() program
11. Write a string endsWith() program
12. Write a string indexOf() program
13. Write a string lastIndexOf() program
14. Write a String substring() program
15. Write a string trim() program


String Buffer Practice Programs
1. Write a StringBuffer insert() program
2. Write a StringBuffer delete() program
3. Write a StringBuffer append() program
4. Write a StringBuffer replace() Program


String Builder Practice Programs
1. Write a StringBuilder length program
2. Write a StringBuilder charAt() method program
3. Write a StringBuilder toLowerCase() Program
4. Write a StringBuilder toUpperCase() program
5. Write a StringBuilder concat() program
6. Write a StringBuilder compareTo program
7. Write a StringBuilder compareToIgnoreCase() Program
8. Write a StringBuilder equals program
9. Write a StringBuilder equalsIgnoreCase() program
10. Write a StringBuilder startsWith() program
11. Write a StringBuilder endsWith() program
12. Write a StringBuilder indexOf() program
13. Write a StringBuilder lastIndexOf() program
14. Write a StringBuilder substring() program
15. Write a StringBuilder trim() program
16. Write a StringBuilder insert() program
17. Write a StringBuilder delete() program
18. Write a StringBuilder append() program
19. Write a StringBuilder replace() Program


Strings Practice Programs with Solutions
1. Write a Java-program to Count the number of vowels in a given string ?
2. Write a Java-program to Count the number of Consonants in a given string ?
3. Write a Java-program to Count the number of spaces in a given string
4. Write a Java-program to Count the number of special characters in a given string ?
5. Write a Java-program to Count the number of letters in a given string ?
6. Write a Java-program to Count the number of Numbers in a given string ?
7. Write a Java-program to Remove vowels in a given string ?
8. Write a Java-program to Remove consonants in a given string ?
9. Write a Java-program to Remove special characters in a given string ?
10. Write a Java-program to Remove spaces in a given string ?
11. Write a Java-program to Remove letters in a given string ?
12. Write a Java-program to Remove numbers in a given string ?
13. Write a Java-program to print duplicate characters from the string ?
14. Write a Java-program to print unique(distinct (or non-repeating characters) ) characters from the string ?
15. Write a java program How to check if two strings are anagrams of each other?
16. How to reverse a string in Java without using the reverse method ?
17. How to count the occurrence of the given character in a string?
18. How to check if a string is a palindrome?
19. How to count the number of words in a given string sentence?
20. How to reverse the words from the given string sentence?
21. How to swap two strings without using the third variable?
MSK TechnologiesTrainings