「Java入門」startsWith()とendsWith()を利用するサンプルコード

Javaコード
package itstudy;
public class StartWithDemo {

public static void main(String[] args) {

String strOrg = “this is test original string";
String strCtn = “original";
if (strOrg.startsWith(strCtn)) // startsWith()
strOrg = strOrg.substring(strCtn.length());
else if (strOrg.endsWith(strCtn)) // endWith()
strOrg = strOrg.substring(0, strOrg.length() – strCtn.length());
else {
int index = strOrg.indexOf(strCtn); // indexOf()
if (index != -1) {
String s2 = strOrg.substring(0, index);
String s3 = strOrg.substring(index + strCtn.length());
strOrg = s2 + s3;
} else
System.out.println(“string/" + strCtn + “/" + “not found");
}
System.out.println(strOrg);
}

}
実行結果
this is test string

Java

Posted by arkgame