replaceAll public String replaceAll(String regex, String replacement) Replaces each substring of this string that matches the given regular expression with the given replacement. An invocation of this method of the form str.replaceAll(regex, repl) yields exactly the same result as the expression Pattern.compile(regex).matcher(str).replaceAll(repl)Parameters: regex - the regular expression to which this string is to be matched Returns: The resulting String Throws: PatternSyntaxException - if the regular expression's syntax is invalid Since: 1.4 See Also: Pattern
String newStr = oldStr.replaceAll("\"",""); //这个是把所有的 " 都去掉.
if(str.indexOf("\"")==0) str = str.substring(1,str.length()); //去掉第一个 "
if(str.lastIndexOf("\"")==(str.length()-1)) str = str.substring(0,str.length()-1); //去掉最后一个 "
import java.util.regex.Pattern;public class T { /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String s="\"aasd\"a\"";
String result=null;
Pattern p=Pattern.compile("(?m)^\"(.+)\"$");
Matcher m=p.matcher(s);
if(m.find()){
result=m.group(1);
}
System.out.println(result);
}}
只去掉开头的
public String replaceAll(String regex,
String replacement)
Replaces each substring of this string that matches the given regular expression with the given replacement.
An invocation of this method of the form str.replaceAll(regex, repl) yields exactly the same result as the expression Pattern.compile(regex).matcher(str).replaceAll(repl)Parameters:
regex - the regular expression to which this string is to be matched
Returns:
The resulting String
Throws:
PatternSyntaxException - if the regular expression's syntax is invalid
Since:
1.4
See Also:
Pattern
直接 str = str.substring(1,str.length()-1);
不就完了
先判断 然后substring