变量名.replace(".","_")
我也是初学都不一定对你查一下JDK的文档因该是用REPLACE这个方法的

解决方案 »

  1.   

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

  2.   

    为提高效率,未使用正则,用一个方法实现(参数合法性未检测) private static String format(String args){
    char[] chResult = new char[args.length()+2];
    for(int i=0,j=0;i<args.length()-1;i++){
    if(args.charAt(i)>='0' && args.charAt(i)<='9'){
    chResult[j++]=args.charAt(i);
    }else{
    chResult[j++]='_';
    chResult[j++]=args.charAt(++i);
    }
    }
    return new String(chResult,0,chResult.length);
    }
      

  3.   

    总结一下,你的要求是把字符串的'.','(', 换成字符'_',对吗?既然这样算法就比较好写了?String对象有如下的方法   String replace(char oldChar, char newChar) 
    String newString=yourString.replace('.','_');
    newString=yourString.replace('(','_');
      

  4.   

    楼上做法比较可取啊
    做两次REPLACE咯
      

  5.   

    String str="..1..3(21))))";
            String result=null;
            Pattern pt=Pattern.compile("(\\(|\\.|\\))");
            Matcher mt=pt.matcher(str);
            result=mt.replaceAll("_");
            int pos=-1;
            for(int i=0;i<result.length();i++)
            {
                if((char)result.charAt(i)!='_')
                {
                    pos=i;
                    break;
                }
            }
            
            if(pos!=-1)
                result=result.substring(pos,result.length());
            pos=-1;
            for(int i=result.length()-1;i>=0;i--)
            {
                if((char)result.charAt(i)!='_')
                {
                    pos=i;
                    break;
                }            
            }
            
            if(pos!=-1)
                result=result.substring(0,pos+1);
            System.out.println(result);