Java 程序可移除字符串开始和结尾处的空格
利用 trim() 方法可移除字符串开始和结尾处的空格。
假设以下字符串包含空格。
str1 = " The Big Bang Theory ";
现在,我们来去掉开始和结尾处的空格。
String str2 = str1.trim();
以下是最终示例及输出。
示例
public class Demo {
public static void main(String[] args) {
String str1 = " The Big Bang Theory ";
System.out.println("String: "+str1);
String str2 = str1.trim();
System.out.println("Updated string (trimming spaces): "+str2);
}
}输出
String: The Big Bang Theory Updated string (trimming spaces): The Big Bang Theory
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP