Java サンプル - 文字列比較
次の例では、文字列関数 compareTo(string)、compareToIgnoreCase(String)、および compareTo(object string) を使用して 2 つの文字列を比較し、文字列内の最初の文字の ASCII の差を返します。
サンプルコードは次のとおりです。
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()));
}
}
上記のサンプルコードの出力結果は次のとおりです。
-32 0 0その他の拡張
Java サンプル