【问题标题】:How to concatenate/combine two attributed strings?如何连接/组合两个属性字符串?
【发布时间】:2016-09-02 11:18:57
【问题描述】:

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

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"
}

我需要做的是将所有这些术语和上标组合成以下有序格式:

词+上标+词+上标

但是,上标必须是超标(因此我使用了 AttributedStrings)。

【问题讨论】:

    标签: java string-concatenation


    【解决方案1】:

    抱歉,据我所知,没有简单的方法可以做到这一点。您可以执行以下操作:

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

    【讨论】:

    • 嗯,最后两行。是否将属性添加到新属性字符串中的每个字符?因为,(看我的例子),术语字符串不会被制作成上标,
    • 还是保留哪个字符有哪个属性?
    • 它只保留每个属性字符串的属性。如果您的 AttributedString 在其不同部分具有不同的属性,并且您事先不知道每个部分的开始/结束位置,则可以对每个 AttributedString 进行第二次传递并分别为每个字符添加属性。它既不高效也不漂亮,但我看不到任何其他方式。
    • 抱歉耽搁了,出了点事。不过,这将是有问题的。我的意思是我正在做的是创建化学电子和惰性气体配置,这可能会变得非常长。我想知道是否可以通过获取我的上标的长度,将其附加到属性字符串,然后使用该长度,从末尾开始,返回该长度,并将所有这些字符更改为上标...
    • 问题的根源在于,您无法更改 AttributedString 的字符串内容。因此,您应该首先完全构建您的字符串,使用该字符串创建一个 AttributedString,然后再添加属性。
    【解决方案2】:

    上面的代码不起作用,因为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);
        }
    
    }
    

    还有一个实用类:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-11-04
      • 2022-11-16
      • 2012-10-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多