どうも、ちょげ(@chogetarou)です。
replaceAll()を使って文字列(string)の末尾を置換する方法を紹介します。
方法

replaceAll()を使って文字列(string)の末尾を置換するには、正規表現を使います。
まず、文字列からreplaceAll()を呼び出します。
そして、replaceAll()の第1引数に「”.$”」、第2引数に置換後の文字を指定します。
//text=対象の文字列、replace=置換後の文字列
String result = text.replaceAll(".$", replace);
上記のreplaceAll()は、呼び出した文字列の末尾の文字を置換した文字列を生成します。
使用例
public class Main {
public static void main(String[] args) throws Exception {
String text = "Hello,World";
String result = text.replaceAll(".$", "*");
System.out.println(result);
}
}
出力:
Hello,Worl*
コメント