Python中删除字符串后面的空格符用 rstrip()
Python中删除字符串前面的空格符用 lstrip()
Python中删除字符串后面的空格符用 strip()
str = " Python "
str1 = str.rstrip() # str1 = " Python"
str2 = str.lstrip() # str2 = "Python "
str3 = str.strip() # str3 = “Python”
Note:
Java中删除字符串前后的空格用trim(),
但是trim()只能去掉字符串中前后的半角空格,而无法去掉全角空格。
去掉全角空格需要在trim()方法的基础上加上一些判断。
String textContent ="abctest “;
textContent = textContent.trim();
while (textContent.startsWith(” “)) {//这里判断是不是全角空格
textContent = textContent.substring(1, textContent.length()).trim();
}
while (textContent.endsWith(” ")) {
textContent = textContent.substring(0, textContent.length() - 1).trim();
}