java过滤string所有空格

来源:百度知道 编辑:UC知道 时间:2024/05/13 12:10:43
import java.io.*;
import java.lang.String;

class HtmlTokenizer extends StreamTokenizer {
//定义各标记,这里的标记仅是本例中必须的,
可根据需要自行扩充\
static int HTML_TEXT=-1;
static int HTML_UNKNOWN=-2;
static int HTML_EOF=-3;
static int HTML_IMAGE=-4;
static int HTML_FRAME=-5;
static int HTML_BACKGROUND=-6;
static int HTML_APPLET=-7;

boolean outsideTag=true; //判断是否在标记之中

//构造器,定义该令牌流的语法表。
public HtmlTokenizer(BufferedReader r) {
super(r);
this.resetSyntax(); //重置语法表\
this.wordChars(0,255); //令牌范围为全部字符\
this.ordinaryChar(’<’); //HTML标记两边的分割符\
this.ordinaryChar(’>’);
} //end of constructor
public int nextHtml(){
int token; //令牌
try{
switch(token=this.nextToken()){
case StreamTokenizer.TT_EOF:
//如果已读到流的尽头,则返回TT_EOF
return HTML_EOF;
ca

protected boolean allWhite(String s){ //过滤所有空格\
if (s.indexOf(" ") != -1) {
String s2 = s.replaceAll(" ", "");
System.out.println("去掉空格后:" + s2);
return true;
} else {
System.out.println("没有空格");
return false;
}}

楼上的,trim()好象只能去两边的空格,字符串中间的空格去不掉.

个人认为使用replaceAll比较好,如果字符串中有全角和半角空格的区别,那就要replaceAll两次,一次是全角一次是半角

trim()

String a = "lo o k";
System.out.println(a.trim());
打印出look;
trim()这个方法是JAVA内置去空格的方法

String s1="";
for(int i=0;i<s.length();i++){
if(s.substring(i,i+1).equals(" ")){
System.out.println("发现空格");
}else{
s1=s1+s.substring(i,i+1);
}
}
return s1;

我测试了的可以用