java字符串去掉空格
来源:互联网 发布:如何催乙方进度 知乎 编辑:程序博客网 时间:2024/06/05 20:42
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字符串——去掉空格trim()
- java去掉字符串空格的方法
- Java去掉字符串两边的空格
- JAVA去掉一个字符串的前后空格
- JAVA中去掉字符串空格的方法
- Java去掉字符串中空格,换行,制表符
- 去掉字符串中的空格
- 去掉字符串空格
- 去掉字符串中空格
- js去掉字符串空格
- 去掉字符串多余空格
- nginx+fastdfs 502异常解决
- 对不起,我的博客
- mysql timeStamp默认值0000-00-00 00:00:00 报错
- 吴恩达Coursera深度学习课程 DeepLearning.ai 提炼笔记(1-4)-- 深层神经网络
- Photon_使用NHibernate中事物防止脏数据_007
- java字符串去掉空格
- My first code--login interface
- git客户端工具sourceTree 无需安装需要注册atlassian账号
- BeagleBone Black 从零到一 (2 MLO、U-Boot)
- 整数中x出现的次数
- Android开发艺术探索_Android的Drawable(六)
- Android Studio中使用Git暂存代码
- C++ 类声明 类前置声明范例
- 什么是餐饮小票打印机?如何对接餐厅软件?有哪些用途