Java 字符串替换空格为加号:replaceAll() 方法详解
可以使用 Java 中 String 类的 replaceAll() 方法来将空格替换成 '+' 号。具体代码如下:
String str = 'This is a test string';
String newStr = str.replaceAll(' ', '+');
System.out.println(newStr); // 输出:This+is+a+test+string
在上面的代码中,将字符串中的空格替换成了 '+' 号,并将结果输出。其中,replaceAll() 方法接受两个参数,第一个参数是要替换的字符串,第二个参数是替换成的字符串。在这里,我们将空格作为要替换的字符串,将 '+' 号作为替换成的字符串。
原文地址: https://www.cveoy.top/t/topic/ogj1 著作权归作者所有。请勿转载和采集!