Java去除字符串最前面一个字符和最后面一个字符
public class demo {
public static void main(String[] args) {
String returnInfo = "\"{\"success\":true,\"msg\":\"success\",\"data\":{}}\"";
System.out.println(returnInfo);
if (returnInfo.startsWith("\"")) {
returnInfo = replaceFirst(returnInfo, "\"", "");
System.out.println("去除前引号:" + returnInfo);
}
if (returnInfo.endsWith("\"")) {
returnInfo = replaceLast(returnInfo, "\"", "");
System.out.println("去除后引号:" + returnInfo);
}
System.out.println(returnInfo);
}
//去掉最前面的一个字符
private static String replaceFirst(String ret, String strToReplace, String replaceWithThis) {
return ret.replaceFirst(strToReplace, replaceWithThis);
}
//去掉最后面的一个字符
public static String replaceLast(String text, String strToReplace, String replaceWithThis) {
return text.replaceFirst("(?s)" + strToReplace + "(?!.*?" + strToReplace + ")", replaceWithThis);
}
}