1 29 30 package echo2example.email; 31 32 import java.text.CharacterIterator ; 33 import java.text.StringCharacterIterator ; 34 35 38 public class MessageUtil { 39 40 51 public static final String clean(String source, int wordLengthLimit, int lengthLimit) { 52 if (source == null) { 53 return null; 54 } 55 StringBuffer out = new StringBuffer (); 56 CharacterIterator ci = new StringCharacterIterator (source); 57 int wordLength = 0; 58 int textLength = 0; 59 char ch = ci.first(); 60 while (ch != CharacterIterator.DONE) { 61 if (lengthLimit > 0) { 62 if (textLength > lengthLimit) { 63 out.append("..."); 64 break; 65 } 66 ++textLength; 67 } 68 if (wordLengthLimit > 0) { 69 if (Character.isWhitespace(ch)) { 70 wordLength = 0; 71 } else { 72 ++wordLength; 73 } 74 if (wordLength > wordLengthLimit) { 75 wordLength = 0; 76 out.append(" "); 77 } 78 } 79 if (ch >= 0x20) { 80 out.append(ch); 81 } 82 ch = ci.next(); 83 } 84 return out.toString(); 85 } 86 87 88 private MessageUtil() { } 89 } 90 | Popular Tags |