- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to initialize and compare strings?
Following example compares two strings by using str compareTo (string), str compareToIgnoreCase(String) and str compareTo(object string) of string class and returns the ascii difference of first odd characters of compared strings.
Example
public class StringCompareEmp{ public static void main(String args[]) { String str = "Hello World"; String anotherString = "hello world"; Object objStr = str; System.out.println( str.compareTo(anotherString) ); System.out.println( str.compareToIgnoreCase(anotherString) ); System.out.println( str.compareTo(objStr.toString())); } }
Output
The above code sample will produce the following result.
-32 0 0
String compare by equals()
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.
Example
public class StringCompareequl{ 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)); } }
Output
The above code sample will produce the following result.
true false
String compare by == operator
Example
public class StringCompareequl{ public static void main(String []args) { String s1 = "tutorialspoint"; String s2 = "tutorialspoint"; String s3 = new String ("Tutorials Point"); System.out.println(s1 == s2); System.out.println(s2 == s3); } }
Output
The above code sample will produce the following result.
true false
- Related Articles
- How to Initialize and Compare Strings in Java?
- How to Initialize and Compare Strings in C#?
- How to declare and initialize constant strings in C#?
- How to compare strings in Java?
- How to compare date strings in Python?
- How to compare two strings in Golang?
- How to compare two strings in Perl?
- Java Program to Compare Strings
- How to compare two strings using regex in Python?
- Java Program to Compare Two Strings
- Golang program to compare two strings
- How do I compare strings in Java?
- Compare Strings in Arduino
- How to compare two strings without case sensitive in Java
- How to compare two strings which are numbers in MySQL?

Advertisements