正如标题所述,如何连接两个属性字符串?
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)。
答案 0 :(得分:2)
很抱歉,但就我所知,没有简单的方法可以做到。您可以执行以下操作:
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());
答案 1 :(得分:0)
上面的代码不起作用,因为getAttributes()方法只返回迭代中当前char的属性 以下是我如何解决它:
我制作了自己的字符串构建器 请注意我在字符串之间添加了一个空格
public class AttributedStringBuilder{
private AttributedString builString;
public AttributedStringBuilder(){
this.builString = new AttributedString("");
}
public void append(AttributedStringBuilder strings){
if(strings == null){
return;
}
this.append(strings.getBuilStirng());
}
public void append(AttributedString string){
if(string == null){
return;
}
this.builString = AttributedStringUtil.concat(this.builString, string," ");
}
public AttributedString getBuilStirng(){
return this.builString;
}
@Override
public String toString(){
return AttributedStringUtil.getString(this.builString);
}
}
和一个util类:
import java.text.AttributedCharacterIterator;
import java.text.AttributedString;
import java.text.CharacterIterator;
public class AttributedStringUtil {
public static AttributedString concat(AttributedString first,AttributedString secound,String seperation){
String firstString = AttributedStringUtil.getString(first);
String secoundString = AttributedStringUtil.getString(secound);
String resultString = firstString + seperation + secoundString;
AttributedString result = new AttributedString(resultString);
AttributedStringUtil.addAttributes(result, first, secound, seperation.length());
return result;
}
public static AttributedString concat(AttributedString first,AttributedString secound){
return AttributedStringUtil.concat(first, secound,"");
}
private static void addAttributes(AttributedString result,AttributedString first,AttributedString secound,int seperationOffset){
AttributedCharacterIterator resultIterator = result.getIterator();
AttributedCharacterIterator firstIterator = first.getIterator();
AttributedCharacterIterator secoundIterator = secound.getIterator();
char resultCharacter = resultIterator.current();
int truePosition = 0;
int usePosition = 0;
while( resultCharacter != CharacterIterator.DONE)
{
usePosition = truePosition;
AttributedCharacterIterator it = AttributedStringUtil.getIterator(firstIterator, secoundIterator);
if(it == null){
break;
}
if(it == secoundIterator){
usePosition += seperationOffset;
}
result.addAttributes(it.getAttributes(), usePosition, usePosition+1);
resultCharacter = resultIterator.next();
it.next();
truePosition ++;
}
}
private static AttributedCharacterIterator getIterator(AttributedCharacterIterator firstIterator, AttributedCharacterIterator secoundIterator){
if(firstIterator.current() != CharacterIterator.DONE){
return firstIterator;
}
if(secoundIterator.current() != CharacterIterator.DONE){
return secoundIterator;
}
return null;
}
public static String getString(AttributedString attributedString){
AttributedCharacterIterator it = attributedString.getIterator();
StringBuilder stringBuilder = new StringBuilder();
char ch = it.current();
while( ch != CharacterIterator.DONE)
{
stringBuilder.append( ch);
ch = it.next();
}
return stringBuilder.toString();
}
}