7. replace ( )

replace ( ) method of String class in Java is generally used for replacing the occurrences of the specified characters with a new character. This method returns a new String by replacing all the old specified characters with new specified characters which are passed as an argument.

Method Signature

String replace (char old, char new)

Example I:
class StringDemo {
public static void main(String args[]) {
String a=new String("ELEPHANT");
String b=a.replace('E','X');
System.out.println(b); } }

Output: XLXPHANT

Example II:
class StringDemo
{
public static void main(String args[]) {
String a="Hello";
System.out.println(a.replace('l','w')); }
}

Output: Hewwo

8. indexOf ( )

indexOf ( ) method of Java String class is used to return the index or first occurrence of any specified character or a substring. indexOf ( ) method returns the integer value 1 when the character is found and it returns -1 when the specified character is not found. Counting of index position in a string starts with zero ( 0 ) index.

Method Signature
int indexOf ( String a )

Example I:

class StringDemo {
public static void main(String args[]) {
String a="Hello";
System.out.println(a.indexOf("l")); } }

Output: 2

Example 2:
class StringDemo {
public static void main(String args[]) {
String a="HELLO MY DEAR STUDENTS";
System.out.println(a.indexOf("LL"));
System.out.println(a.indexOf("DEAR"));
System.out.println(a.length()); } }

Output:

2
9
22

9. substring ( )

substring ( ) method of String class is used to return the part of the specified string according the index position passed as an argument to the substring()

Method Signature
String substring ( int begin_index) String substring(int begin_index, int end_index)

Example I:

class StringDemo {
public static void main(String args[]) {
String a="HELLO";
System.out.println(a.substring(2));
System.out.println(a.substring(2,4)); } }

Output:

LLO
LL

Let us understand the concept through the given diagram below.

10. equals ( )

Java String class equals ( ) method is used for comparing two specified strings based on the content of the string. If all the characters are matched of one string to another then it returns a true value otherwise it returns false value. It compares the string to the specified object.

Method Signature

public boolean equals ( Object another_object)

Example I

class StringDemo { public static void main(String args[]) { String a=new String("Hello"); String b=new String("Hello"); String c=new String("hello"); System.out.println(a.equals(b)); System.out.println(a.equals(c)); } }

Output:

true
false

11. equalsIgnoreCase ( )

Java String class equalsIgnoreCase ( ) method is used for comparing two specified strings based on the content of the string ignoring the case. If all the characters are matched of one string to another ignoring its case (uppercase or lowercase) then it returns a true value otherwise it returns false value. It compares the string to the specified object.

Method Signature
public boolean equals ( Object another_object)

Example I:

class StringDemo { public static void main(String args[]) { String a=new String("Hello"); String b=new String("Hello"); String c=new String("hello"); System.out.println(a.equals(b)); System.out.println(a.equalsIgnoreCase(c)); } }

Output:

true
true

What is difference between == operator and .equals ( ) method of String class.

Double equal to operator (==) is used for reference comparison and .equals ( ) method is used for content comparison.

As we all know that Object class is the parent class of all the classes. Object class also contains a equals ( ) method which is used for reference comparison but when we call equals ( ) method of String class which is child of Object class then equals ( ) method is overridden for content comparison based on our requirement.

Example I:

class StringDemo { public static void main(String args[]) { String a=new String("Hello"); String b=new String("Hello"); String c=new String("hello"); System.out.println(a==b); System.out.println(a.equals(b)); System.out.println(a.equals(c)); System.out.println(a.equalsIgnoreCase(c)); } }

Output:

false
true
false
true

Example II:

class StringDemo { public static void main(String args[]) { String a="Hello"; String b="Hello"; String c="hello"; System.out.println(a==b); System.out.println(a.equals(b)); System.out.println(a.equals(c)); System.out.println(a.equalsIgnoreCase(c)); } }

Output:

true
true
false
true

12. compareTo ( )

compareTo ( ) method of String class is used to compare two strings lexicographically. The comparison is done on the basis of Unicode value of each character in the String. However, if both the string are equal then it returns 0 else it return either positive or negative value.

  • if first_string > second_string, then it returns any Positive value
  • if first_string < second_string, then it returns any Negative value
  • if first_string == second_string, then it returns zero value
Method Signature

int compareTo ( String str)

Program to print Unicode value of any character

class StringDemo { public static void main(String args[]) { String a="Hello"; String b="Hello"; System.out.println("Unicode value of H="+(int)'H'); System.out.println("Unicode value of e="+(int)'e'); System.out.println("Unicode value of l="+(int)'l'); System.out.println("Unicode value of l="+(int)'l'); System.out.println("Unicode value of o="+(int)'o'); } }

Output:

Example I:

class StringDemo { public static void main(String args[]) { String a="Hello"; String b="Hello"; String c="hello"; String d="Hi"; System.out.println(a.compareTo(b)); System.out.println(a.compareTo(c)); System.out.println(d.compareTo(a)); System.out.println(a.compareTo(d)); } }

Output:

Let us understand the logic behind the comparison. Suppose there are two Strings as:

String str1="Hello";
String str2="hello";
System.out.println(str1.compareTo(str2));
System.out.println(str2.compareTo(str1));

Let us understand the concept through the given diagram below.

To print unicode of any character we have to typecast the character into int type as: System.out.println( (int) ‘H’ ); and so on
  • First character of both string is matched first, if it is equal then next character is compared so on till the end on the basis of unicode value
  • str1.compareTo(str2) then it returns -32 because if we compare as: (str1<str2) then it returns the negative value which is the difference of unicode value of the character i.e (72-104)= -32
  • If we compare as str2.compareTo(str1) then it returns 32 because when we compare as: str1>str2 then it returns the positive value which is the difference of unicode value of the character i.e. (104-72)=32

Example II.

class StringDemo {
public static void main(String args[]) {
String a="Hello";
String b="";
System.out.println(a.compareTo(b)); } }

Output:

5

Explanation: If the second string is empty then it will return the total length of the first string while comparison and if the first string is empty then it will return the negative total length of the second string while comparison.

Example III.

class StringDemo {
public static void main(String args[]) {
String a="";
String b="Hello";
System.out.println(a.compareTo(b)); } }

Output: -5

Difference between equals ( ) and compareTo ( ) methods

equals ( ) compareTo ( )
equals ( ) method only checks
whether two strings are identical or not.
compareTo ( ) method checks
whether a string is equal,
bigger or smaller than other
string or not.
It returns boolean value i.e (true/false)It returns an integer type value.
Example:
String a=”Hello”;
String b=”Hello;
System.out.println(a.equals(b));
Output: true
Example:
String a=”Hello”;
String b=”Hello”;
System.out.println(a.compareT o(b));
Output: 0