Opening Hours :7AM to 9PM
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
It is also predefiend class in java Language in java.lang package. Whose object is immutable( Value can not be changed )
String s="Value"; // Datatype or String objref=new String("Value"); // ObjectExample:
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.
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()
1.String insert()
2.String delete()
3.String append()
4.String replace()
Both String and StringBuffer Methods
Which can be used to find number of characters of given string.
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()); } }
Which can be used to find the cheracter at the given index value.
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)); } }
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.
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"); } } }
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.
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"); } } }
Which can be used to combine two strings into a single string.
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)); } }
Which returns true if the string is starts with given substring otherwise return false
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")); } }
Which returns true if the string is ends with given substring otherwise return false
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")); } }
Which can be used to compare two strings(content).Which returns true both the strings are same otherwise returns false.
String objref1=new String("Value"); String objref2=new String("Value"); objref1.equals(objref2);Note:
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"); } } }
Which can be used to compare two strings(content).Which returns true both the strings are same otherwise returns false.But here ignore case.
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"); } } }
Which can be used to get the index value from the given string or character(first occurence)
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')); } }
Which can be used to get the index value from the given string or character(last occurence)
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')); } }
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.
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.
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)); } }
public class Main { public static void main(String args[]) { String s1=new String("sateesh"); System.out.println(s1.substring(2,5)); } }
Which can be used to convert upper case to lower case
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()); } }
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()); } }
It is used to trim the starting and ending spaces from the given strings
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()); } }
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)
StringBuffer objref=new StringBuffer("Value");
which can be used to insert either a new characer or string at given index value.
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')); } }
public class Main { public static void main(String args[]) { StringBuffer s1=new StringBuffer("sateesh "); System.out.println(s1.insert(7,"kumar")); } }
Which can be used to delete either character or string from the main string.
1. deleteCharAt()
2. delete()
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)); } }
public class Main { public static void main(String args[]) { StringBuffer s1=new StringBuffer("sateesh "); System.out.println(s1.delete(1,3)); } }
Which can be used to add a new String at the end of existing string.
objref.append("String value");
public class Main { public static void main(String args[]) { StringBuffer s1=new StringBuffer("sateesh "); System.out.println(s1.append("kumar")); } }
Which can be used to replace a new String in to old string based on the index value.
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")); } }
It is a same as StringBuffer class but only difference is StringBuffer is synchronized where as StringBuilder is non synchronized.
StringBuilder objref=new StringBuilder("Value");
StringBuilder supports all the methods of String and StringBuffer.