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.
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.
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);
}
String newString=yourString.replace('.','_');
newString=yourString.replace('(','_');
做两次REPLACE咯
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);