• iconJava Online Training In Andhra Pradesh and Telangana
  • icon9010519704

Opening Hours :7AM to 9PM

Strings


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";
or
String objref=new String("Value");
Example:
Datatype:
String s1="Sateesh";
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







String Length:

Which can be used to find number of characters of given string.
Syntax:
s.length();


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:
s.charAt(indexvalue);


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 on zero integer value.
Syntax:
int n=s1.compareTo(s2);


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:
int n=s1.compareToIgnoreCase(s2);


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:
s1.concat(s2);


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:
s1.startsWith("substring");


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:
s1.endsWith("substring");


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:
s1.equals(s2);
Note:
It is case sensitive method.


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:
s1.equalsIgnoreCase(s2);


cclass 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");
}
}
}
Note: what is difference between equals() and == ?
equals always comes the content whereas == compare the reference;
Excample:
String s1="Hello";
String s2=new String("Hellow");
s1.equals(s2); -- true
s1==s2; -- false (references of both the strings different)


Output:
Both Are Equal
String IndexOf

Which can be used to get the index value from the given string or character(first occurence)
Syntax:
s1.indexOf('character');
s1.indexOf("String");


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)
s1.LastIndexOf('character');
s1.LastIndexOf("String");


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:
s.substring(index);
2.substring("starting index,ending index): it can be used to get the specific value from given string.
Example:
s1.substring(starting index,ending index);


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


Output:
String ToLowerCase

Which can be used to convert upper case to lower case
Syntax:
s1.tolowerCase();


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


Output:
String ToUpperCase

Which can be used to convert lower case to upper case
Syntax:
s1.tolowerCase();


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


Output:
String Trim

It is used to trim the starting and ending spaces from the given strings
Syntax:
s1.trim();


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


Output:
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:
sb.insert(index,'char');
sb.insert(index,"String");


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


Output:
String Delete

Which can be used to delete either character or string from the main string.
Syntax:
1.deleteCharAt(index); // Delete One Character
2.delete(start index,ending index); Delete Multiple Characters


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


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

Note:
In higher version of java delete(index) is replaced by deleteCharAt(index).


Output:
String Append

Which can be used to add a new String at the end of existing string.
Syntax:
append("String value");


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


Output:
String Replace

Which can be used to replace a new String in to old string based on the index value.
Syntax:
replace(starting index,ending index,"new String")


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


Output:

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.