JAVA中去掉空格 trim函数 其他代码
2010-01-07 17:26
288 查看
JAVA中去掉空格
1. String.trim()
trim()是去掉首尾空格
2.str.replace(" ", ""); 去掉所有空格,包括首尾、中间
String str = " hell o ";
String str2 = str.replaceAll(" ", "");
System.out.println(str2);
3.或者replaceAll(" +",""); 去掉所有空格
4.str = .replaceAll("\\s*", "");
可以替换大部分空白字符, 不限于空格
\s 可以匹配空格、制表符、换页符等空白字符的其中任意一个
5.或者下面的代码也可以去掉所有空格,包括首尾、中间
public String remove(String resource,char ch)
{
StringBuffer buffer=new StringBuffer();
int position=0;
char currentChar;
while(position<resource.length())
{
currentChar=resource.charAt(position++);
if(currentChar!=ch) buffer.append(currentChar); } return buffer.toString();
}
测试的全部代码如下:
public class test1 {
public static void main(String[] args) {
String QJstr = " hello wch ";
String QJstr1 = remove(QJstr,' ');
System.out.println(QJstr + "\n" + QJstr1);
}
public static String remove(String resource,char ch)
{
StringBuffer buffer=new StringBuffer();
int position=0;
char currentChar;
while(position<resource.length())
{
currentChar=resource.charAt(position++);
if(currentChar!=ch) buffer.append(currentChar);
}
return buffer.toString();
}
}
1. String.trim()
trim()是去掉首尾空格
2.str.replace(" ", ""); 去掉所有空格,包括首尾、中间
String str = " hell o ";
String str2 = str.replaceAll(" ", "");
System.out.println(str2);
3.或者replaceAll(" +",""); 去掉所有空格
4.str = .replaceAll("\\s*", "");
可以替换大部分空白字符, 不限于空格
\s 可以匹配空格、制表符、换页符等空白字符的其中任意一个
5.或者下面的代码也可以去掉所有空格,包括首尾、中间
public String remove(String resource,char ch)
{
StringBuffer buffer=new StringBuffer();
int position=0;
char currentChar;
while(position<resource.length())
{
currentChar=resource.charAt(position++);
if(currentChar!=ch) buffer.append(currentChar); } return buffer.toString();
}
测试的全部代码如下:
public class test1 {
public static void main(String[] args) {
String QJstr = " hello wch ";
String QJstr1 = remove(QJstr,' ');
System.out.println(QJstr + "\n" + QJstr1);
}
public static String remove(String resource,char ch)
{
StringBuffer buffer=new StringBuffer();
int position=0;
char currentChar;
while(position<resource.length())
{
currentChar=resource.charAt(position++);
if(currentChar!=ch) buffer.append(currentChar);
}
return buffer.toString();
}
}
相关文章推荐
- JAVA中去掉空格 trim函数 其他代码
- JAVA中去掉空格 trim函数 其他代码
- java去掉文本中多余的空格与空行实例代码
- JAVA中去掉空格trim函数的方法
- 通过Java正则表达式去掉SQL代码中回车换行和多余空格
- 利用Java正则表达式去掉SQL代码中回车换行和多余空格
- 通过Java正则表达式去掉SQL代码中回车换行和多余空格
- 去掉中文空格的PHP正则代码
- java中去掉字符串中的空格及空白字符
- Js 去掉字符串中的空格(实现代码)
- java执行命令或调用其他exe程序的代码范例
- 去掉中文空格的PHP正则代码
- [c++]输入一行字符,分别统计出其中英文字母、空格、数字和其他字符的个数。(写的代码有疑惑,求解求教)
- 去掉中文空格的PHP正则代码
- 将字符串 “ hello word,你 好 世 界 ! ” 两端空格去掉并且将其中的其他所有空格替换成一个空格 输出结果为“hello word,你 好 世界”
- JAVA中去掉空格
- JAVA中去掉字符串空格的方法
- JAVA中去掉空格经典整理
- 用Java统计字符串中的字母、数字、空格和其他字符
- JAVA中去掉空格