如何 concatenate/combine 两个属性字符串?

How to concatenate/combine two attributed strings?

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

AttributedStrings 不包含concat 方法,当然concat 的short-cut(字符串上的+ 运算符)也不起作用。

使用 ctrl+F 在 AttributedString javadocs 上搜索 "concat"...javadocs 甚至没有提到 concat,也没有提到任何组合两个属性字符串的方法(https://docs.oracle.com/javase/7/docs/api/java/text/AttributedString.html).


具体我的最终愿望:

假设我有 2 个 objects,每个都有 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());

上面的代码不起作用,因为getAttributes()方法returns在迭代中只获取当前字符的属性 这是我的解决方法:

我制作了自己的字符串生成器 请注意,我在字符串

之间添加了一个 space
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);
    }

}

和一个实用程序 class:

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();
    }
}