Java tutorial
//package com.java2s; public class Main { private static StringBuffer breakUpFFox(String str, int chars) { StringBuffer sb = new StringBuffer(); int z = 0; for (int i = 0; i < str.length(); i++) { switch (str.charAt(i)) { case ' ': z = 0; sb.append(str.charAt(i)); break; /*case '-': sb.append(str.charAt(i)); int q = str.indexOf(" ", i); int p = str.indexOf("-", i); if (q == -1 && p == -1) { sb.append("​"); z = 0; } else if (q > (chars - z) && p > (chars - z)) { sb.append("​"); z = 0; } else { z++; } break;*/ case '&': sb.append("&"); if (str.length() > i + 4 && !str.substring(i + 1, i + 4).equals("amp;")) { sb.append("amp;"); } z++; break; default: sb.append(str.charAt(i)); z++; } if (z > 0 && z % chars == 0) { sb.append(" "); } } return sb; } }