trim() 函数只能去掉英文空格,如果是中文空格不知如何办了???急!

解决方案 »

  1.   

    楼主所说的中文空格是全角的,如果是半角空格的话,照样可以trim()
    建议楼主再trim之前,用替换语句replaceAll,把全角空格替换成半角空格再处理~~~
      

  2.   

    奇怪,我用的全部过滤掉了
    public class EmptyLence {
      public static void main(String[] args){
       String a="我爱你abc啊啊啊啊 ";
       String b="abc ";
       System.out.println(a.trim());
       System.out.println(a);
       System.out.println(b.trim());
       System.out.println(b);
      }
    }
      

  3.   

    public class Test {
    public static void main(String[] args) {
                 String str ="tang ";
                  str = str.replaceAll(" "," ");
                  str= str.trim();
    System.out.println(str.length());
    }
    }学习了
      

  4.   

    chuanyuan88(船员) ( ) :你用的半角吧
      

  5.   

    如下:不管是全角还是半角空格都去掉了
    String strAAA = " ab ";
    strAAA = strAAA.replaceAll("^[ | ](.*)[ | ]$","$1");
      

  6.   

    shi zhe yang de  ma ?
      

  7.   

    import java.util.StringTokenizer;/**
     * @author chuayuan88 
     * 说明:该类实现把字符中的全角转化为半角,包括全角字符,全角空格.
     * 在windows中,中文和全角字符都占两个字节,并且使用了ascii chart 2 (codes 128?C255)。
     * 我们可以凭这一点来一个个检测用户输入的是否是中文和全角字符。
     * 实际上,全角字符的第一个字节总是被置为163,而第二个字节则是相同半角字符码加上128(包  * 括空格)。
     * 如半角a为65,则全角a则是163(第一个字节)、193(第二个字节,128+65)。 而对于中文来讲,*它的第一个字节被置为大于163,
     * (如'阿'为:176 162),我们可以在检测到中文时不进行转换。
     */
    public class EmptyLence { private static int b2i(byte n) {
    int m = n;
    if (n < 0) {
    m = n + 256;
    return m;
    }
    return m;
    } private static byte i2b(int n) {
    byte m = (byte) n;
    if (n > 127) {
    m = (byte) (n - 256);
    return m;
    }
    return m;
    } private static byte[] getOutputList(byte[] m) { //过滤byte数组中为0的元素
    int len = m.length;
    StringBuffer temp = new StringBuffer(1024);
    for (int i = 0; i < len - 1; i++) {
    if (m[i] != 0) {
    temp.append(m[i]);
    temp.append(",");
    }
    }
    temp.append(m[len - 1]);
    StringTokenizer st = new StringTokenizer(temp.toString(), ",");
    int total = st.countTokens();
    byte[] newList = new byte[total];
    for (int i = 0; i < total; i++) {
    newList[i] = (byte) Integer.parseInt(st.nextToken());
    }
    return newList; } public static void main(String[] args) {
    String oldStr = "abc def 中国队";
    int n = oldStr.getBytes().length; 
    byte[] strList = new byte[n + 1]; 
    for (int j = 0; j < n; j++) {
    strList[j] = oldStr.getBytes()[j];
    }
    byte[] newList = new byte[n];
    int c1, c2;
    for (int i = 0; i < newList.length; i++) {
    c1 = b2i(strList[i]);
    c2 = b2i(strList[i + 1]); if (c1 == 163) { //判断是否为全角字符
    newList[i] = i2b(c2 - 128);
    i++;
    continue;
    } if (c1 > 163) { //判断是否为汉字
    newList[i] = i2b(c1);
    newList[i + 1] = i2b(c2);
    i++;
    continue;
    } if ((c1 == 161) && (c2 == 161))//全角空格是个特例,另加处理
    {
    newList[i] = 32;
    i++;
    continue;
    }
    newList[i] = i2b(c1); } byte[] outputList = getOutputList(newList);
    System.out.println(new String(outputList));

    }}
      

  8.   

    to chuanyuan88(船员):
    你的程序怎么编译过不去呢?jcreator下编译:
    String oldStr = "锝侊絺锝冦??锝勶絽锝嗐??涓浗闃?";
                    ^
    1 errordos下编译:
    C:\work>javac EmptyLence.java
    EmptyLence.java:53: unclosed string literal
    String oldStr = "锝侊絺锝冦??锝勶絽锝嗐??涓浗闃?";
                    ^
    1 error为什么呢?
      

  9.   

    String oldStr = "锝侊絺锝冦??锝勶絽锝嗐??涓浗闃?";你这里怎么是乱码呢?可能是乱码引起的错误,oldStr里放的是测试信息,里头有全角的空格和全角的字母,还有半角的字母,和汉字;
    String oldStr = "abc def 中国队";