In this article we are going to discuss a problem from Java String; this is one of the most commonly asked interview question of today. Write a program in Java to compare two Strings. .
 

Write a program in Java to compare two Strings.

e.g. if given string is "tbNext" and ?tbNext? than compare these strings for ASCII values..
We can compare two strings using compareTo() and compareToIgnoreCase () functions as shown in below code:
package com.tbNext.examples;

public class CompareTwoStrings {

	public static void main(String[] args) {
		String s1 = "tbNext";
		String s2 = "tbNext";
		String s3 = "Tech Burps";

		System.out.println(s1.compareTo(s2));
		System.out.println(s1.compareToIgnoreCase(s2));
		System.out.println(s1.compareToIgnoreCase(s3));

	}

}

Output:

32 0 66


That's it, we saw Write a program in Java to compare two Strings. In upcoming articles we will see more about Java and Related programming problems and solutions.
  • By Techburps.com
  • Apr 16, 2016
  • Programming