StringTokenizer is split into "<br/">"
I may be stupid, but I do not understand why the behavior of StringTokenizer is here:
import static org.apache.commons.lang.StringEscapeUtils.escapeHtml; String object = (String) value; String escaped = escapeHtml(object); StringTokenizer tokenizer = new StringTokenizer(escaped, escapeHtml("<br/>")); If fx. value
Hej<br/>$user.get(0).name Har vundet<br/><table border='1'><tr><th>Name</th><th>Played</th><th>Brewed</th></tr>#foreach( $u in $user )<tr><td>$u.name</td> <td>$u.played</td> <td>$u.brewed</td></tr>#end</table><br/> Then the result
Hej $use . e (0).name Ha vunde a eo de ='1' h Name h h P ayed h h B ewed h #fo each( $u in $use ) d $u.name d d $up ayed d d $u. ewed d #end a e It makes no sense to me.
How can I make him behave as I expect.
From the documentation :
The characters of the delim argument are delimiters for separating tokens. Symbols of the separator itself will not be considered as tokens.
In other words, the characters that StringTokenizer when to separate a string:
- <
- b
- t
- / <Lithium →
When it matches any of these characters in a string (the escaped variable in your code), the StringTokenizer instance breaks the result and discards the token. You can confirm this by noting that the letter r does not appear on the output.
Use String.split instead of others.
Each character in the string is considered a marker for separation. Thus, your code is broken into each character "&", "l", "t", ";", "b", "r", "/" or "g" (since escapeHtml will replace the character <"and"> "with < and > respectively).
You probably want to use String.split , which takes the regular expression as the item to be broken:
String[] parts = object.split("<br/>"); or
String[] parts = escaped.split(escapeHtml("<br/>")); Just make sure your separator token does not have special regular expression characters.
If you want to separate a line / text with a word and not just a few characters, then you are better off using String.split
I did a test:
public static void main(String[] args){ String s = "Hej<br/>$user.get(0).name Har vundet<br/><table border='1'><tr><th>Name</th><th>Played</th><th>Brewed</th></tr>#foreach( $u in $user )<tr><td>$u.name</td> <td>$u.played</td> <td>$u.brewed</td></tr>#end</table><br/>"; String[] lines = s.split("<br/>"); for(String ss:lines) System.out.println(ss); } and here you have the result:
Hej $user.get(0).name Har vundet <table border='1'><tr><th>Name</th><th>Played</th><th>Brewed</th></tr>#foreach( $u in $user )<tr><td>$u.name</td> <td>$u.played</td> <td>$u.brewed</td></tr>#end</table> Tjena