What Does the compareTo Method Do in Java

Arjun Thakur
Updated on 26-Feb-2020 07:07:03

241 Views

The compareTo() method in Java compares two strings lexicographically.ExampleLive Demopublic class Test {    public static void main(String args[]) {       String str1 = "Strings are immutable";       String str2 = new String("Strings are immutable");       String str3 = new String("Integers are not immutable");       int result = str1.compareTo( str2 );       System.out.println(result);       result = str2.compareTo( str3 );       System.out.println(result);    } }Output0 10

Split Java String into Tokens with StringTokenizer

Abhinanda Shri
Updated on 26-Feb-2020 07:06:25

518 Views

The hasMoreTokens() method is used to test if there are more tokens available from this tokenizer's string.ExampleLive Demoimport java.util.*; public class StringTokenizerDemo {    public static void main(String[] args) {       // creating string tokenizer       StringTokenizer st = new StringTokenizer("Come to learn");       // checking elements       while (st.hasMoreElements()) {          System.out.println("Next element : " + st.nextElement());       }    } }OutputNext element : Come Next element : to Next element : learn

Reverse Each Word in a String using Java

Anjana
Updated on 26-Feb-2020 07:05:44

882 Views

StringBuffer class of the java.lang package provides reverse() method. This method returns a reverse sequence of the characters in the current String. Using this method you can reverse a string in Java.To reverse each word in a string you need to split the string, store it in an array of strings and reverse each word using the reverse() method of the StringBuffer class.ExampleLive Demoimport java.lang.*; public class StringBufferDemo {    public static void main(String[] args) {       StringBuffer buff = new StringBuffer("tutorials point");       System.out.println("buffer = " + buff);       // reverse characters of the ... Read More

Compare Two Strings Without Case Sensitivity in Java

George John
Updated on 26-Feb-2020 07:04:22

2K+ Views

The equalsIgnoreCase() method compares this string to the specified object. The result is true if and only if the argument is not null and is a String object that represents the same sequence of characters as this object.ExampleLive Demopublic class Sample { public static void main(String args[]) { String Str1 = new String("This is really not immutable!!"); String Str2 = Str1; String Str3 = new String("THIS IS REALLY NOT IMMUTABLE!!"); boolean retVal; retVal = Str1.equalsIgnoreCase(Str2); System.out.println("Returned Value = " + retVal ); retVal = Str1.equalsIgnoreCase( Str3 ); System.out.println("Returned Value = " + retVal ); } }OutputReturned Value = true Returned Value = true

Java String Comparison Methods

Kumar Varma
Updated on 26-Feb-2020 06:49:35

638 Views

Java String class provides different comparison methods namely:The compareTo() methodThis method compares two Strings lexicographically. This method returns  a negative integer if current String object lexicographically precedes the argument string.  a positive integer if current String object lexicographically follows the argument  true when the strings are equal.Exampleimport java.lang.*; public class StringDemo {    public static void main(String[] args) {       String str1 = "tutorials", str2 = "point";       // comparing str1 and str2       int retval = str1.compareTo(str2);       // prints the return value of the comparison     ... Read More

Difference between == and equals() Method in Java

Alankritha Ammu
Updated on 26-Feb-2020 06:42:24

3K+ Views

The equals() method compares this string to the specified object. The result is true if and only if the argument is not null and is a String object that represents the same sequence of characters as this object.Example Live Demopublic class Sample {    public static void main(String []args) {       String s1 = "tutorialspoint";       String s2 = "tutorialspoint";       String s3 = new String ("Tutorials Point");       System.out.println(s1.equals(s2));       System.out.println(s2.equals(s3));    } }Outputtrue falseYou can also compare two strings using == operator. But, it compares references of the given ... Read More

Compare Two Strings Lexicographically in Java

Fendadis John
Updated on 26-Feb-2020 06:41:22

7K+ Views

The compareTo() method of the String class. This method compares two Strings lexicographically. The comparison is based on the Unicode value of each character in the strings. The character sequence represented by this String object is compared lexicographically to the character sequence represented by the argument string. This method returnsa negative integer if current String object lexicographically precedes the argument string.a positive integer if current String object lexicographically follows the argumenttrue when the strings are equal.ExampleLive Demoimport java.lang.*; public class StringDemo {    public static void main(String[] args) {       String str1 = "tutorials", str2 = "point";   ... Read More

Check If Two Strings Are Equal in Java

Anjana
Updated on 26-Feb-2020 06:39:51

16K+ Views

You can check the equality of two Strings in Java using the equals() method. This method compares this string to the specified object. The result is true if and only if the argument is not null and is a String object that represents the same sequence of characters as this object.Exampleimport java.lang.* public class StringDemo {    public static void main(String[] args) {       String str1 = "Tutorialspoint";       String str2 = "Tutorialspoint";       String str3 = "Hi";             // checking for equality       boolean retval1 = ... Read More

Use equals and equalsIgnoreCase in Java

Manikanth Mani
Updated on 26-Feb-2020 06:37:53

383 Views

The equals() methodThis method compares this string to the specified object. The result is true if and only if the argument is not null and is a String object that represents the same sequence of characters as this object.Exampleimport java.lang.*; public class StringDemo {    public static void main(String[] args) {       String str1 = "sachin tendulkar";       String str2 = "amrood admin";       String str3 = "amrood admin";             // checking for equality       boolean retval1 = str2.equals(str1);       boolean retval2 = str2.equals(str3);   ... Read More

Compare String Equality in Java

Ayyan
Updated on 26-Feb-2020 06:30:47

366 Views

You can check the equality of two Strings in Java using the equals() method. This method compares this string to the specified object. The result is true if and only if the argument is not null and is a String object that represents the same sequence of characters as this object.Exampleimport java.lang.* public class StringDemo {    public static void main(String[] args) {       String str1 = "Tutorialspoint";       String str2 = "Tutorialspoint";       String str3 = "Hi";             // checking for equality       boolean retval1 = ... Read More

Advertisements