This is a Java program that compares two strings using both the Collator class and the String class.
import java.text.Collator; import java.util.Locale; public class String_Comparison { public static void display(int diff, String s1, String s2) { if (diff < 0) { System.out.println(s1 + " Comes Before " + s2); } else if (diff > 0) { System.out.println(s1 + " Comes After " + s2); } else { System.out.println(s1 + " and " + s2 + " are the Same Strings"); } } public static void main(String[] args) { Locale USL = new Locale("en", "US"); Collator col = Collator.getInstance(USL); String s1 = "Pineapple"; String s2 = "Watermelon"; int diff = col.compare(s1, s2); System.out.print("Comparing Strings using Collator : "); display(diff, s1, s2); System.out.print("Comparing Strings using String : "); diff = s1.compareTo(s2); display(diff, s1, s2); } }
Comparing Strings using Collator : Pineapple Comes Before Watermelon Comparing Strings using String : Pineapple Comes Before Watermelon
Learn All in Tamil © Designed & Developed By Tutor Joes | Privacy Policy | Terms & Conditions