Compare Strings in Java

Kumar Varma
Updated on 26-Feb-2020 07:09:39

797 Views

You can compare two Strings in Java using the compareTo() method, equals() method or == operator.The compareTo() method compares two strings. 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. The result is a negative integer if this String object lexicographically precedes the argument string.The result is a positive integer if this String object lexicographically follows the argument string. The result is zero if the strings are equal, compareTo returns 0 exactly when the equals(Object) method would ... Read More

Delete Specific Record from MySQL Table using AND in WHERE Clause

AmitDiwan
Updated on 26-Feb-2020 07:08:22

188 Views

MySQL AND is used in WHERE to fetch a record by filtering using multiple conditions. Let us first create a table−mysql> create table DemoTable    -> (    -> Id int,    -> Name varchar(20)    -> ); Query OK, 0 rows affected (0.70 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(101, 'Chris'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values(102, 'David'); Query OK, 1 row affected (0.51 sec) mysql> insert into DemoTable values(103, 'Bob'); Query OK, 1 row affected (0.14 sec)Display all records from the table using select statement ... Read More

Toggle Each Word in String Using Java

Alankritha Ammu
Updated on 26-Feb-2020 07:07:50

1K+ Views

You can change the cases of the letters of a word using toUpperCase() and toLowerCase() methods.Split each word in the string using the split() method, change the first letter of each word to lower case and remaining letters to upper case.ExampleLive Demopublic class Sample{    public static void main(String args[]){       String sample = "Hello How are you";       String[] words = sample.split(" ");       String result = "";       for(String word:words){          String firstSub = word.substring(0, 1);          String secondSub = word.substring(1);          result = result+firstSub.toLowerCase()+secondSub.toUpperCase()+" ";       }       System.out.println(result);    } }OutputhELLO hOW aRE yOU

What Does the compareTo Method Do in Java

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

207 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

493 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

828 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

604 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

Advertisements