Advertisement
Guest User

Untitled

a guest
Apr 18th, 2014
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.13 KB | None | 0 0
  1. String[] parts = str.split("&([A-Za-z]+|[0-9]+|x[A-Fa-f0-9]+);");
  2. if(parts.length <= 1) return str; //No matched entities.
  3.  
  4. StringBuilder result = new StringBuilder(str.length());
  5. result.append(parts[0]); //First part always exists.
  6. int pos = parts[0].length + 1; //Skip past the first entity and the ampersand.
  7. for(int i = 1;i < parts.length;i++) {
  8. String entityName = str.substring(pos,str.indexOf(';',pos));
  9. if(entityName.matches("x[A-Fa-f0-9]+") && entityName.length() <= 5) {
  10. result.append((char)Integer.decode("0" + entityName));
  11. } else if(entityName.matches("[0-9]+")) {
  12. result.append((char)Integer.decode(entityName));
  13. } else {
  14. switch(entityName) {
  15. case "euml": result.append('ë'); break;
  16. case "auml": result.append('ä'); break;
  17. ...
  18. default: result.append("&" + entityName + ";"); //Unknown entity. Give the original string.
  19. }
  20. }
  21. result.append(parts[i]); //Append the text after the entity.
  22. pos += entityName.length() + parts[i].length() + 2; //Skip past the entity name, the semicolon and the following part.
  23. }
  24. return result.toString();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement