「Java8」特殊文字パーセント文字列をエスケープする

2022年6月26日

環境
Java8
Eclipse 4.14.0

書式
public static String format(String format,Object… args)
指定された書式の文字列と引数を使って、書式付き文字列を返します。
使用例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
package com.arkgame.info;
public class SekiDemo {
/* %%でパーセント文字列をエスケープ */
private static String ptn = "%%";
public static void main(String[] args) {
String target = "price cost 55";
String strA = String.format("%s", target);
System.out.println("パーセント文字列をエスケープ前: " + strA);
String strB = String.format("%s" + ptn, target);
System.out.println("パーセント文字列エスケープ後: " + strB);
}
}
package com.arkgame.info; public class SekiDemo { /* %%でパーセント文字列をエスケープ */ private static String ptn = "%%"; public static void main(String[] args) { String target = "price cost 55"; String strA = String.format("%s", target); System.out.println("パーセント文字列をエスケープ前: " + strA); String strB = String.format("%s" + ptn, target); System.out.println("パーセント文字列エスケープ後: " + strB); } }
package com.arkgame.info;

public class SekiDemo {

      /* %%でパーセント文字列をエスケープ */
      private static String ptn = "%%";

      public static void main(String[] args) {

            String target = "price cost 55";
            String strA = String.format("%s", target);
            System.out.println("パーセント文字列をエスケープ前: " + strA);

            String strB = String.format("%s" + ptn, target);
            System.out.println("パーセント文字列エスケープ後: " + strB);

      }

}

実行結果

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
%エスケープ前: price cost 55
%エスケープ後: price cost 55%
%エスケープ前: price cost 55 %エスケープ後: price cost 55%
%エスケープ前: price cost 55
%エスケープ後: price cost 55%

 

Java

Posted by arkgame