my code :
class Solution {
public String entityParser(String text) {
Stack<Integer> s=new Stack<>();
Map<String ,String> m=new HashMap<>();
m.put(""","\"");
m.put("‘","\‘");
m.put("&","&");
m.put(">",">");
m.put("<","<");
m.put("⁄","/");
String outputans="";
for(int i=0;i<text.length();i++){
if(text.charAt(i)==‘&‘&&s.isEmpty()){
s.push(i);
}else if(text.charAt(i)==‘&‘&&!s.isEmpty()){
s.clear();
}else if(text.charAt(i)==‘;‘&&!s.isEmpty()){
int loca=s.pop();
String temp=text.substring(loca,i+1);
String ans;
if(m.get(temp)!=null){
ans=m.get(temp);
outputans+=ans;
}else{//"&faflalfla;"的情况
outputans+=temp;
}
}else if(s.isEmpty()){
outputans+=text.charAt(i);
}
}
return outputans;
}
}
someone else‘s:
public String entityParser(String text) {
return text
.replaceAll(""", "\"")
.replaceAll("‘", "‘")
.replaceAll(">", ">")
.replaceAll("<", "<")
.replaceAll("⁄", "/")
.replaceAll("&", "&");
}
更快速的方法:(replace() 方法 速度比replaceAll()的快 replace也是全部替换 但是replaceAll()是有关正则表达式的)
return text.replace(""", "\"").replace("‘", "‘").replace(">", ">") .replace("<", "<").replace("⁄", "/").replace("&", "&");
conclusion;
STUPID GUY!!! please make good use of API!!!
原文:https://www.cnblogs.com/jessekwok/p/12715948.html