- 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
Comparing two Strings lexicographically in Java n
We can compare two strings lexicographically using following ways in Java.
Using String.compareTo(String) method. It compares in a case-sensitive manner.
Using String.compareToIgnoreCase(String) method. It compares in case insensitive manner.
Using String.compareTo(Object) method. It compares in case-sensitive manner.
These methods return the ASCII difference of first odd characters of compared strings.
Example
public class Tester { 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
-32 0 0
- Related Articles
- Compare two strings lexicographically in Java.
- Compare two Strings lexicographically in Java programming
- Compare the two Strings lexicographically in Java
- Java Program to Compare two strings lexicographically
- Comparing two strings in MySQL?
- Comparing two strings in C++
- Compare two strings lexicographically in C#
- Comparing Strings and Portions of Strings in Java
- C++ Program to Compare two strings lexicographically
- Golang program to compare two strings lexicographically
- Comparing Strings with (possible) null values in java?
- Comparing ascii scores of strings - JavaScript
- Compare two Strings in Java
- Comparing two dates in PHP
- Find n-th lexicographically permutation of a strings in Python

Advertisements