Java中怎样将两个字段绑定_java – 如何连接/组合两个属性字符串?

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

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必须是超级脚本(因此我使用的是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,string-concatenation

来源: https://codeday.me/bug/20190702/1352949.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值