昨天参加了智乐的笔试,其中有一题目要求是这样的,本人很菜,不会。请教:写一个方法,把一个语句,例如:“I’m___ very___ happy __to __introduce __myself___ to __you __here”,单词之间的空格不一定是一个。要求字符串调用这个方法后,能把单词之间的空格都改为一个。例如:“I’m_very_happy_to _introduce_myself_to_you_here”

解决方案 »

  1.   

    先判断空格的位置,然后把有空格去掉,然后在加上一个空格可以用StringBuild
      

  2.   

    根据空格split成字符串数组,然后遍历数组去掉空格以后,再在数组中的每个元素里把空格加上就行了
      

  3.   

    一个一个读进来,如果是空格则记住,下边的空格则忽略。
    具体的程序如下:
    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);
        }
    }
    程序写的不好请多包涵。
      

  4.   

    ?"I’m___ very___ happy __to __introduce __myself___ to __you __here".replaceAll("([_ ]+)", " ")
      

  5.   

    中间那些下划线都是空格的意思吧,如果是这样,很简单啊String str = "I’m        very    happy   to  introduce myself   to   you here";
    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());
      

  6.   

    package test1;
     
    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/
      

  7.   


    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
      

  8.   

    汗,上面的多了点东西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+", " "));
        }
    }
      

  9.   

    public static void main(String[] args)
    {
    String s = "I’m   very   happy to      introduce "+
               "myself   to             you         here";
    System.out.println(s.replaceAll("\\s+", " ")); }
      

  10.   

    本身就一个空格间隔的就不需要替换了
    String s = "I’m         very   happy  to    introduce  myself  to  you   here";
    System.out.println(s.replaceAll(" {2,}", " "));
      

  11.   

    我测试了一下,为什么下面两种方式,得到的结果都一样?String s = "I’m         very   happy  to    introduce  myself  to  you   here"; 
    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,}
      

  12.   

    18楼提到的
    s.replaceAll(" {1,}", " "));表示将1个或1个以上的空格符替换成1个空格符
    s.replaceAll(" {2,}", " "));表示将2个或2个以上的空格符替换成1个空格符
    无论是1个或1个以上还是2个或2个以上,效果是一样的
      

  13.   


    int m[];
    int k=0;
    for(int i=0;i<s.length;i++)
    {if(String s.charAt()==" ")k++;
    m[k]=k;
    }//记录空格位置
      

  14.   

    public class filter {
           
    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);
        }
           
    }
    这是我的代码。
      

  15.   

    import java.io.BufferedReader;
    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;

    }
    }
      

  16.   

    个人认为是不是可以做一个队列,然后每读入一个字符都push到这个队列里面,如果是空格,那就取一下队列的队尾,如果是空格,则继续,否则也push到队列里面。