Java replaceAllメソッドでURLの文字列%2Bをプラス記号に変換する

環境
JavaSE 1.8
Eclipse 4.14.0

構文
public String replaceAll(String regex,String replacement)
指定された正規表現に一致する、この文字列の各部分文字列に対し、指定された置換を実行します。
このフォームのメソッド呼び出しstr.replaceAll(regex, repl)では、次の式と正確に同じ結果が得られます。
パラメータ:
regex – この文字列との一致を判定する正規表現
replacement – 一致するものそれぞれに置き換えられる文字列
戻り値:結果となるString

使用例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
package com.arkgame.study;
public class ArkgDemo {
public static void main(String[] args) {
// URLの形式
String enstr = "user%2bbyamada";
System.out.println("%2Bをプラス記号へ変換する結果Before: " + enstr);
// 文字列をデコードする関数を呼び出す
String res = enstr.replaceAll("%2b", "+");
System.out.println("%2Bをプラス記号へ変換する結果End: " + res);
}
}
package com.arkgame.study; public class ArkgDemo { public static void main(String[] args) { // URLの形式 String enstr = "user%2bbyamada"; System.out.println("%2Bをプラス記号へ変換する結果Before: " + enstr); // 文字列をデコードする関数を呼び出す String res = enstr.replaceAll("%2b", "+"); System.out.println("%2Bをプラス記号へ変換する結果End: " + res); } }
package com.arkgame.study;

public class ArkgDemo {

      public static void main(String[] args) {
            // URLの形式
            String enstr = "user%2bbyamada";
            System.out.println("%2Bをプラス記号へ変換する結果Before: " + enstr);
            // 文字列をデコードする関数を呼び出す
            String res = enstr.replaceAll("%2b", "+");
            System.out.println("%2Bをプラス記号へ変換する結果End: " + res);
      }
}

実行結果

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
%2Bをプラス記号へ変換する結果Before: user%2bbyamada
%2Bをプラス記号へ変換する結果End: user+byamada
%2Bをプラス記号へ変換する結果Before: user%2bbyamada %2Bをプラス記号へ変換する結果End: user+byamada
%2Bをプラス記号へ変換する結果Before: user%2bbyamada
%2Bをプラス記号へ変換する結果End: user+byamada

 

Java

Posted by arkgame