如标题所述,如何连接两个属性字符串?
AttributedStrings 不包含 concat 方法,当然 concat 的快捷方式(字符串上的 + 运算符)也不起作用。
使用 ctrl+F 在 AttributedString javadocs 上搜索“concat”...javadocs 甚至没有提到 concat,也没有提到任何组合两个属性字符串的方法(https://docs.oracle.com/javase/7/docs/api/java/text/AttributedString.html)。
具体我的最终愿望:
假设我有 2 个对象,每个对象有 2 个字符串。 (遵循JSON格式)
{
"term" : "1s",
"superScript" : "1"
},
{
"term" : "1s",
"superScript" : "2"
}
我需要做的是以以下有序格式组合所有这些术语和上标:
术语+上标+术语+上标
但是,superScripts 必须是 super 脚本(因此我使用 AttributedStrings)。
最佳答案
抱歉,但据我所知,没有简单的方法可以做到这一点。您可以执行以下操作:
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());
关于java - 如何连接/组合两个属性字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39291094/