将白色 space 替换为 %20

Replace white space with %20

我必须用 %20 替换字符串中的所有 space。

我尝试在这种模式下使用replaceAll方法title.replaceAll(" ", "%20");(显然标题是一个字符串)但这不起作用,结果是全白的初始字符串space

解决方案

不要使用全部替换我发现它永远无法按预期工作。只要 String.replace 就可以很好地完成工作。

public static void main (String [] args) {

    String test = "H E L L O";

    test = test.replace(" ", "%20");
    System.out.println (test);

}

结果

H%20E%20L%20L%20O