JAVA中去掉空格
来源:互联网 发布:网络专题排版设计 编辑:程序博客网 时间:2024/04/30 03:08
JAVA中去掉空格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();
}
-------------------------------Test-----------------------------------------------------
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();
}
-------------------------------Test-----------------------------------------------------
package practisedemo;
public class Demo{
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++);
//如果当前字符不是要去除的字符,则将当前字符加入到StringBuffer中
if(currentChar!=ch) buffer.append(currentChar);
}
return buffer.toString();
}
public static void main(String args[]) {
String str= "afdsfdf 了 2012 0407 Alamps 老师";
char c=' ';
System.out.println("结果为:"+Demo.remove(str, c));
}
}
public class Demo{
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++);
//如果当前字符不是要去除的字符,则将当前字符加入到StringBuffer中
if(currentChar!=ch) buffer.append(currentChar);
}
return buffer.toString();
}
public static void main(String args[]) {
String str= "afdsfdf 了 2012 0407 Alamps 老师";
char c=' ';
System.out.println("结果为:"+Demo.remove(str, c));
}
}
阅读全文
0 0
- JAVA中去掉空格
- JAVA中去掉空格
- JAVA中去掉空格
- JAVA中去掉空格
- JAVA中去掉空格
- JAVA中去掉空格
- JAVA中去掉空格
- java中如何去掉字符串中的空格
- java中去掉字符串中间的空格
- Java中如何去掉字符串中的空格
- JAVA中去掉字符串空格的方法
- JAVA中去掉字符串空格的方法
- java 中去掉空格的方法
- Java去掉字符串中空格,换行,制表符
- JAVA String 中去掉空格的方法
- 去掉字符串中空格
- 去掉字符串中空格
- QtString中去掉空格
- bind的工作原理
- 与C++标准委员会专家、侯捷等大师面对面,是怎样一种体验?
- QT开发的程序的发布
- sublime text shortcut
- java.lang.IllegalStateException:Unable to create schema compiler
- JAVA中去掉空格
- windows进程的相关概念
- JDK源码ScheduledExecutorService解读
- 大话设计模式总结2-策略模式
- [ 线段树 单调队列 ] Codeforces859F Ordering T-Shirts
- JAVA是引用传递还是值传递
- 数据库指令集合
- 系统安全性之十大措施
- VIM的使用总结