As the title states, how does one concatenate two attributed strings?
The AttributedStrings does not contain the concat method, and of course the short-cut of concat ( + operator on strings) does not work either.
Using ctrl+F to search for "concat" on the AttributedString javadocs... The javadocs don't even mention concat, nor does it appear to mention any means to combine two attributed strings (https://docs.oracle.com/javase/7/docs/api/java/text/AttributedString.html).
Specifics on my end desire:
Let's say I have 2 objects each with 2 strings. (Following JSON format)
{
"term" : "1s",
"superScript" : "1"
},
{
"term" : "1s",
"superScript" : "2"
}
What I need to do is combine all of these terms and superscripts in the following, ordered format:
term+superscript+term+superscript
However, the superScripts must be super scripts (hence my use of AttributedStrings).
Sorry but as far as I know, there is no easy way to do it. You can do something like the following:
AttributedCharacterIterator aci1 = attributedString1.getIterator();
AttributedCharacterIterator aci2 = attributedString2.getIterator();
StringBuilder sb = new StringBuilder();
char ch = aci1.current();
while( ch != CharacterIterator.DONE)
{
sb.append( ch);
ch = aci1.next();
}
ch = aci2.current();
while( ch != CharacterIterator.DONE)
{
sb.append( ch);
ch = aci2.next();
}
AttributedString combined = new AttributedString( sb.toString());
combined.addAttributes( aci1.getAttributes(), 0, aci1.getEndIndex());
combined.addAttributes( aci2.getAttributes(), aci1.getEndIndex(), aci1.getEndIndex() + aci2.getEndIndex());