code_junkie

How to rewrite this block of code using a StringBuilder in Java?

Nov 12th, 2011
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.61 KB | None | 0 0
  1. String word = "foobooandfoo";
  2. String converted = "";
  3. converted = word.replaceAll("[ao]", "1");
  4. converted = converted.replaceAll("[df]", "2");
  5. converted = converted.replaceAll("[n]", "3");
  6.  
  7. public static void translate(StringBuilder str, char[] table)
  8. {
  9. for (int idx = 0; idx < str.length(); ++idx) {
  10. char ch = str.charAt(idx);
  11. if (ch < table.length) {
  12. ch = table[ch];
  13. str.setCharAt(idx, ch);
  14. }
  15. }
  16. }
  17.  
  18. public static void translate(StringBuilder str, Map<Character, Character> table)
  19. {
  20. for (int idx = 0; idx < str.length(); ++idx) {
  21. char ch = str.charAt(idx);
  22. Character conversion = table.get(ch);
  23. if (conversion != null)
  24. str.setCharAt(idx, conversion);
  25. }
  26. }
  27.  
  28. StringBuilder result = new StringBuilder(word.length());
  29.  
  30. for (char c : word.toCharArray()) {
  31. switch (c) {
  32. case 'a': case 'o': result.append('1'); break;
  33. case 'd': case 'f': result.append('2'); break;
  34. case 'n': result.append('3'); break;
  35. default: result.append(c); break;
  36. }
  37. }
  38.  
  39. public String convert(String text)
  40. {
  41. char[] chars = new char[text.length()];
  42. for (int i=0; i < text.length(); i++)
  43. {
  44. char c = text.charAt(i);
  45. char converted;
  46. switch (c)
  47. {
  48. case 'a': converted = '1'; break;
  49. case 'o': converted = '1'; break;
  50. case 'd': converted = '2'; break;
  51. case 'f': converted = '2'; break;
  52. case 'n': converted = '3'; break;
  53. default : converted = c; break;
  54. }
  55. chars[i] = converted;
  56. }
  57. return new String(chars);
  58. }
  59.  
  60. String word = "foobooandfoo";
  61. String converted = word.replaceAll("[ao]", "1")
  62. .replaceAll("[df]", "2")
  63. .replaceAll("[n]", "3");
  64.  
  65. String text = "foobooandfoo";
  66. Pattern p = Pattern.compile("([ao])|([df])|n");
  67. Matcher m = p.matcher(text);
  68. StringBuffer sb = new StringBuffer();
  69. while (m.find())
  70. {
  71. m.appendReplacement(sb, "");
  72. sb.append(m.start(1) != -1 ? '1' :
  73. m.start(2) != -1 ? '2' :
  74. '3');
  75. }
  76. m.appendTail(sb);
  77. System.out.println(sb.toString());
  78.  
  79. // usage:
  80. Map<String, String> replaceRules = new HashMap<String, String>();
  81. replaceRules.put("ao", "1");
  82. replaceRules.put("df", "2");
  83. replaceRules.put("n", "3");
  84. String s = replacePartsOf("foobooandfoo", replaceRules);
  85.  
  86. // actual method
  87. public String replacePartsOf(String thisString, Map<String, String> withThese) {
  88. for(Entry<String, String> rule : withThese.entrySet()) {
  89. thisString = thisString.replaceAll(rule.getKey(), rule.getValue());
  90. }
  91.  
  92. return thisString;
  93. }
Add Comment
Please, Sign In to add comment