Home > Java > javaTutorial > body text

Java Example - String Comparison

黄舟
Release: 2017-02-21 09:54:31
Original
1546 people have browsed it

In the following example, we use the string functions compareTo (string), compareToIgnoreCase(String) and compareTo(object string) to compare two strings and return the ASCII difference of the first letter in the string.

The example code is as follows:

//StringCompareEmp.java 文件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()));
   }}
Copy after login

The output result of the above code example is:

-32
0
0
Copy after login

The above is the Java example - the content of string comparison, more For related content, please pay attention to the PHP Chinese website (www.php.cn)!


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template