java - 如何连接/组合两个属性字符串?

标签 java string-concatenation

如标题所述,如何连接两个属性字符串?

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/

相关文章:

java - 检查 android Realm 中的唯一值

java - 对一个数组进行排序,其中多个连续的索引号形成一个应该放在一起的条目

javascript - 使用 "+ "运算符推送有什么区别吗?

javascript - 语法错误: missing ) after argument list (line 28, file “Code.gs” )

vba - Excel VBA - 带分隔符和单引号的字符串连接

使用 strcat 和 realloc 的串联产生意外错误

java - 动态创建 JPanel

java - 如何使用 Selenium Webdriver 获取当前窗口大小?

java - 无法找到逻辑名称为 @embeddedid 的列

sql - 如何连接来自 SQL Server XML 查询的数据?