public class testString
{
public static void main(String[] args) 
{
String testStr = " d d ";
char[] c = new char[5];
int j=0;
for(int i=0; i < testStr.length(); i++)
{
if(!(testStr.charAt(i) == ' '))
{
c[j] = testStr.charAt(i);
j++;
}
}
testStr = new String(c);
System.out.println(testStr);
}
}

解决方案 »

  1.   

    java.lang.String:String replace(char oldChar, char newChar) 
              Returns a new string resulting from replacing all occurrences of oldChar in this string with newChar. String replaceAll(String regex, String replacement) 
              Replaces each substring of this string that matches the given regular expression with the given replacement. String replaceFirst(String regex, String replacement) 
              Replaces the first substring of this string that matches the given regular expression with the given replacement. 
      

  2.   

    替换是一种方案,老版本的jdk没有replace方法,就用这个吧,出自他人之手,不错
    ============================
    public static String stringReplace(String __strSourceString, String __strOldString, String __strNewString)
    {
    String strReturnString = __strSourceString;
    int nLength = 0;
    if(__strOldString != null)
    {
    nLength = __strOldString.length();
    }
    if(strReturnString != null && strReturnString.length() > nLength)
    {
    int max = 0;
    String strAllStep = "";
    for(int i = 0; i < __strSourceString.length(); i++)
    {
    max = i + __strOldString.length() > __strSourceString.length()? __strSourceString.length():i + nLength;
    String strOneStep = __strSourceString.substring(i, max);
    if(!strOneStep.equals(__strOldString))
    {
    strAllStep += strOneStep.substring(0,1);
    }else{
    strAllStep += __strNewString;
    i += nLength -1 ;
    }
    }
    strReturnString = strAllStep;
    }
    return strReturnString;
    }
    ==============
    下面的也可以,不过有些局限性,不过解决你的问题应该可以
    =================
    String strIn1 = "hello. Today \"I am \"";
    System.out.println(strIn1);
    StringTokenizer oToken1 = new StringTokenizer(strIn1);
    String strResult = "";
    while(oToken1.hasMoreElements())
    {
    strResult += oToken1.nextToken();
    }
    strResult是结果