2016-09-02 17 views
3

タイトルには、2つの属性付き文字列をどのように連結するのでしょうか?2つの属性付き文字列を連結/結合する方法は?

AttributedStringsにはconcatメソッドが含まれていません。もちろん、concat(文字列の+演算子)のショートカットも機能しません。

AttributedString javadocsで "concat"を検索するためにctrl + Fを使用する... javadocsにはconcatについて言及していないし、2つの属性付き文字列(https://docs.oracle.com/javase/7/docs/api/java/text/AttributedString.html)を組み合わせる手段はありません。私の最後の願いに


細目:

のは、私は2列と2つのオブジェクトそれぞれを持っているとしましょう。 (JSON形式に続いて)私がする必要がどのような

{ 
    "term" : "1s", 
    "superScript" : "1" 
}, 
{ 
    "term" : "1s", 
    "superScript" : "2" 
} 

は以下にこれらの用語と上付き文字のすべてを兼ね備えている、フォーマットを命じ:

しかし

用語+上付き+長期+上付き

、スーパースクリプトはスーパースクリプトでなければなりません(したがって私のAttributedStringsの使用)。

答えて

2

申し訳ありませんが、私が知る限り、簡単な方法はありません。あなたは、次のような何かを行うことができます。

を:のgetAttributes()メソッドは、相続人は、私はそれを解決する方法反復 にのみ、現在の文字の属性を返すため

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

hmmm、そこの最後の2行です。新しい属性付き文字列内のすべての文字に属性を追加していますか? (私の例を見て)、用語の文字列は上付き文字である – Tyler

+0

に投稿されるのではなく、どの文字がどの属性を持つかを保持していますか? – Tyler

+1

属性付き文字列ごとに属性を保持するだけです。 AttributedStringが異なる部分に異なる属性を持ち、あらかじめ各部分の開始位置と終了位置がわからない場合は、各AttributedStringに対して2回目のパスを作成し、各文字の属性を個別に追加することができます。それは効率的でも美しくもないでしょうが、私は他の方法を見ません。 – uoyilmaz

0

上記のコードは、動作しません。

:私は、文字列

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のクラスの間にスペースを追加し、私自身の文字列ビルダ ノートを作りました

関連する問題