请写一段程序,删除字符串空格内部空格.(不要用函数!!)

解决方案 »

  1.   

    ?不用函数?
    那么你用StringBuilder 然后循环每个字符串的字符,如果不是空格则加到builder里面
    最后输出 .toString就行了,。
      

  2.   

    String str="a b c d e f g";
    String result="";
    for(int i=0;i<str.length();i++){
      char chr=str.charAt(i);
      if(chr!=' '){
        result+=chr;
      }
    }
    System.out.println(result);//abcdefg
      

  3.   

    还是用StringBuilder好,最好不要用String的连接符(+)
      

  4.   

    import java.util.regex.Matcher;
    import java.util.regex.Pattern;public class StringUtil { 
    public static void replaceBlank()
    {
       Pattern p = Pattern.compile("\\s*|\t|\r|\n");
       String str="I am a, I am Hello ok, \n new line ffdsa!";
       System.out.println("before:"+str);
       Matcher m = p.matcher(str);
       String after = m.replaceAll(""); 
       System.out.println("after:"+after);
    }public static void main(String[] args) {
         replaceBlank();
       }} 
      

  5.   

    package csdn;import java.util.*;public class DeleteSpace { public static void main(String[] args) {
    // TODO Auto-generated method stub
    String str = " a c  c w 1  34 9";
    List list = new ArrayList();
    for (int i = 0; i < str.length(); i++) {
    char c = str.charAt(i);
    if (c != ' ') {
    list.add(c);
    }
    }
    System.out.println(list.toString());
    }}
      

  6.   


    package csdn;import java.util.*;public class DeleteSpace { public static void main(String[] args) {
    // TODO Auto-generated method stub
    String str = " a c  c w 1  34 9";
    List list = new ArrayList();
    for (int i = 0; i < str.length(); i++) {
    char c = str.charAt(i);
    if (c != ' ') {
    list.add(c);
    }
    }
    System.out.println(list.toString());
    }}
      

  7.   

    public class TestChar {
    public static void main(String[] agars){
    StringBuilder sb=new StringBuilder();
    String s=new String("d dk djkf g jh");
    for(char ss:s.toCharArray()){
    if(ss!=' '){
    sb.append(ss);
    }
    }
    System.out.println(sb);
    }
    }
      

  8.   

    其实方法还有很多,楼上都有正解
    我用另一种方法试试看,直接以空格为分隔符把字符串分割成数组,然后拼接数组
    StringBuffer sb = new StringBuffer();
    String s = "a b c f";
    String[] ss = s.split(" ");
    for(int i=0;i<ss.length;i++){
     sb.append(ss[i]);
    }
      

  9.   

    public static void main(String[] args) {

    String s = " saef wet ewt wet  " ;
    s  = s.trim().replace(" ", "");
    System.out.println(s);
    }
      

  10.   


    main()函数也不能用吗?程序里不能出现括号?
      

  11.   


    用replaceAll("\\s| ","");|后的空的地方就是一个全角空格.
      

  12.   

    怎么到10楼以后才有人提到replaceAll呢
      

  13.   

     
    public class TrimTest
    {
        
        public static void main(String[] args)
        {
    System.out.println( "ab  s f ".length() );
    System.out.println( trim( " ab     s   f " ) );
    System.out.println( trim( " ab     s   f " ).length() );
        }    public static String trim( String s )
        {
    char[] a = s.toCharArray();
    int N = 0;
    for( int i = 0; i < s.length(); i++ )
    {
        a[N] = a[i];
        if(a[N] != 32)N++;
    }

    return new String( a, 0, N );
        }
        
     )