昨天参加了智乐的笔试,其中有一题目要求是这样的,本人很菜,不会。请教:写一个方法,把一个语句,例如:“I’m___ very___ happy __to __introduce __myself___ to __you __here”,单词之间的空格不一定是一个。要求字符串调用这个方法后,能把单词之间的空格都改为一个。例如:“I’m_very_happy_to _introduce_myself_to_you_here”
调试欢乐多
具体的程序如下:
package test;public class SpaceTrimTest { public static void main(String[] args) { String str = "I’m very happy to introduce myself to you here";
char c;
boolean bl_thefirstspace = true;
StringBuffer sb = new StringBuffer(); for (int i = 0; i < str.length(); i++) { c = str.charAt(i);
if (c == ' ') {
if (bl_thefirstspace) {
bl_thefirstspace = false;
sb.append(c);
}
} else {
bl_thefirstspace = true;
sb.append(c);
}
}
System.out.println(sb);
}
}
程序写的不好请多包涵。
String[] strArray = str.split("\\s+");
StringBuilder sbd = new StringBuilder(strArray[0]);
for(int i=1; i<strArray.length; i++){
sbd.append(' ');
sbd.append(strArray[i]);
}
System.out.println(sbd.toString());
public class Test6
{
public static void main(String[] args)
{
String s="I’m very happy to introduce myself to you here";
String s2=s.replaceAll("\\s{1,}", " ");
System.out.println(s2);
}
} 欢迎光临我的博客 http://blog.csdn.net/zhuche110/
public class TestRegex { public static void main(String[] args) { String string="I’m very happy to introduce myself to you here";
System.out.println(string.replaceAll("\\s+", " "));
}
}output:
I’m very happy to introduce myself to you here
System.out.println(string.replaceAll("\\s+", " "));
}
}
{
String s = "I’m very happy to introduce "+
"myself to you here";
System.out.println(s.replaceAll("\\s+", " ")); }
String s = "I’m very happy to introduce myself to you here";
System.out.println(s.replaceAll(" {2,}", " "));
System.out.println(s.replaceAll(" {2,}", " ")); //我这里用的{2,}String s = "I’m very happy to introduce myself to you here";
System.out.println(s.replaceAll(" {1,}", " ")); //我这里用的{1,}
s.replaceAll(" {1,}", " "));表示将1个或1个以上的空格符替换成1个空格符
s.replaceAll(" {2,}", " "));表示将2个或2个以上的空格符替换成1个空格符
无论是1个或1个以上还是2个或2个以上,效果是一样的
int m[];
int k=0;
for(int i=0;i<s.length;i++)
{if(String s.charAt()==" ")k++;
m[k]=k;
}//记录空格位置
public static void filter(String s)
{
StringBuilder str=new StringBuilder();
for(int i=0;i<s.length();i++)
{
if(' '!=s.charAt(i))
{
str.append(s.charAt(i));
}
else
{
if(i!=0)
str.append(' ');
for(++i;i<s.length();i++)
{
if(' '!=s.charAt(i))
{
str.append(s.charAt(i));
break;
}
}
}
}
System.out.println(str.toString());
}
public static void main(String[] args)
{
String s="I’m very happy to introduce myself to you here!";
filter(s);
}
}
这是我的代码。
import java.io.InputStream;
import java.io.InputStreamReader;public class zhile {
//写一个方法,把一个语句,例如:
//“I’m___ very___ happy __to __introduce __myself___ to __you __here”,
// 单词之间的空格不一定是一个。要求字符串调用这个方法后,能把单词之间的空格都改为一个。
// 例如:“I’m_very_happy_to _introduce_myself_to_you_here” public static void main(String[] args){
// String a="dsfdggg";
//// a.indexOf()
// System.out.print(a.indexOf("d",2));
try{
System.out.println("input a sentence:");
BufferedReader input=new BufferedReader( new InputStreamReader(System.in));
String strOut=strReturn(input.readLine());
System.out.println(strOut);
}
catch(Exception e){
}
}
private static String strReturn(String inputStr){
int flag=0;
String strFront;
String strBack;
for(int i=0;i<inputStr.length();i++){
//如果是第一个空格
if (inputStr.charAt(i)==' ' && flag==0){
strFront=inputStr.substring(0,i);
strBack=inputStr.substring(i);
flag=1;
inputStr=strFront+strBack;
continue;
}
//如果是连续空格
if (inputStr.charAt(i)==' ' && flag==1){
strFront=inputStr.substring(0,i);
strBack=inputStr.substring(i+1);
flag=1;
inputStr=strFront+strBack;
i--;
continue;
}
//如果不是空格
if (inputStr.charAt(i)!=' '){
strFront=inputStr.substring(0,i);
strBack=inputStr.substring(i);
flag=0;
inputStr=strFront+strBack;
continue;
}
}
return inputStr;
}
}