import java.io.*;
class StringRd
{
public static void main(String[] args) throws IOException
{
String a="i'm OK!";
StringReader srd=new StringReader(a);
int rd;
int cont=0;
int pureWdCont=0;
String contRsut;
while ((rd=srd.read())!=-1)
{
cont++;
if (rd!=0)//不知道空格的转义字符是什么?
{
pureWdCont++;
}
}
contRsut="count is "+cont;
System.out.println(contRsut);
String pureWdContRsut="pure word count is "+pureWdCont;
System.out.println(pureWdContRsut);
}
}
4 个解决方案
#1
空格的转义字符是
32
32
#2
String a = "i'm OK!";
int m ,n;
while(true){
m = a.length();
a = a.replaceAll(" ","");
n = a.length();
if(m==n){
System.out.println(n);
break;
}
}
}
int m ,n;
while(true){
m = a.length();
a = a.replaceAll(" ","");
n = a.length();
if(m==n){
System.out.println(n);
break;
}
}
}
#3
直接用临时变量保存去掉所有空格的字符串就行了啊
上面那个循环都没必要
直接这样就可以
String a = "i'm OK!";
String tmp = a;
tmp = tmp.replaceAll(" ","");
System.out.println(tmp.length());
上面那个循环都没必要
直接这样就可以
String a = "i'm OK!";
String tmp = a;
tmp = tmp.replaceAll(" ","");
System.out.println(tmp.length());
#4
Character.isWhiteSpace(char c);
#1
空格的转义字符是
32
32
#2
String a = "i'm OK!";
int m ,n;
while(true){
m = a.length();
a = a.replaceAll(" ","");
n = a.length();
if(m==n){
System.out.println(n);
break;
}
}
}
int m ,n;
while(true){
m = a.length();
a = a.replaceAll(" ","");
n = a.length();
if(m==n){
System.out.println(n);
break;
}
}
}
#3
直接用临时变量保存去掉所有空格的字符串就行了啊
上面那个循环都没必要
直接这样就可以
String a = "i'm OK!";
String tmp = a;
tmp = tmp.replaceAll(" ","");
System.out.println(tmp.length());
上面那个循环都没必要
直接这样就可以
String a = "i'm OK!";
String tmp = a;
tmp = tmp.replaceAll(" ","");
System.out.println(tmp.length());
#4
Character.isWhiteSpace(char c);