Java8 StringクラスはendsWith()メソッドの使い方

環境
JavaSE 1.8
Eclipse 4.14.0

関数
public boolean endsWith​(String suffix)
この文字列が、指定された接尾辞で終るかどうかを判定します。
パラメータ:
suffix – 接尾辞。
戻り値:
引数によって表される文字シーケンスが、このオブジェクトによって表される文字シーケンスの接尾辞である場合はtrue、そうでない場合はfalse。
引数が空の文字列の場合や、equals(Object)メソッドによる判定においてこのStringオブジェクトに等しい場合の結果はtrueになる。

使用例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
package com.arkgame.study;
public class TestDemo {
public static void main(String[] args) {
String str = "ARKGAME";
// 例1
boolean resA = str.endsWith("E");
System.out.println("Eで終わるか: "+resA);
// 例2
boolean resB = str.endsWith("A");
System.out.println("Aで終わるか: "+resB);
// 例3
boolean resC = str.endsWith("ME");
System.out.println("MEで終わるか: "+resC);
}
}
package com.arkgame.study; public class TestDemo { public static void main(String[] args) { String str = "ARKGAME"; // 例1 boolean resA = str.endsWith("E"); System.out.println("Eで終わるか: "+resA); // 例2 boolean resB = str.endsWith("A"); System.out.println("Aで終わるか: "+resB); // 例3 boolean resC = str.endsWith("ME"); System.out.println("MEで終わるか: "+resC); } }
package com.arkgame.study;

public class TestDemo {

      public static void main(String[] args) {

            String str = "ARKGAME";
            // 例1
            boolean resA = str.endsWith("E");
            System.out.println("Eで終わるか: "+resA);

            // 例2
            boolean resB = str.endsWith("A");
            System.out.println("Aで終わるか: "+resB);

            // 例3
            boolean resC = str.endsWith("ME");
            System.out.println("MEで終わるか: "+resC);

      }

}

実行結果
Eで終わるか: true
Aで終わるか: false
MEで終わるか: true

Java

Posted by arkgame