Java字符串編輯(修剪字符串)
修剪字符串
可以使用trim()方法從字符串中刪除所有前導和尾隨空格和控制字符。
trim()方法刪除字符串中所有前導和尾隨字符,其Unicode值小於\u0020(十進制32)。 例如,
替換字符串的一部分
replace()方法將舊字符和新字符作爲參數。它通過將新字符替換所有的舊字符並返回一個替換後新的String對象。 例如,
public class Main {
public static void main(String[] args) {
String oldStr = new String("tooth");
String newStr = oldStr.replace('o', 'e');
System.out.println(newStr);
}
}
上面的代碼生成以下結果。
teeth