1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Javaで比較対象の文字列が同じか判定する

Last updated at Posted at 2018-07-20

文字列(String)を比較するには、値の大小ではなく、equalsメソッドを使用します。

String型変数A.equals(String型変数B)

String型変数に「.equals」を続けます、そして()の中に比較対象のString型変数をそのまま指定します。
「String型変数AとString型変数Bが等しいかどうか」という内容になりますので、等しい場合はtrueが、等しくない場合はfalseが返ります。

サンプルコード:

HelloWorld.java
public class HelloWorld{

     public static void main(String []args){
        
        String str1 = "abc";
        String str2 = "Abc";

        System.out.println(str1.equals(str2));
    
     }
}
結果:
$ javac HelloWorld.java
$ java HelloWorld
false
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?