2016-09-02 1 views
3

Comme l'indique le titre, comment concaténer deux chaînes attribuées?Comment concaténer/combiner deux chaînes attribuées?

AttributedStrings ne contient pas la méthode concat, et bien sûr, le raccourci de concat (+ opérateur sur les chaînes) ne fonctionne pas non plus. Utiliser ctrl + F pour rechercher "concat" sur les javadocs AttributedString ... Les javadocs ne mentionnent même pas concat, et ne semblent pas non plus mentionner de moyen de combiner deux chaînes attribuées (https://docs.oracle.com/javase/7/docs/api/java/text/AttributedString.html).


sur mon désir Specifics final:

Disons que j'ai 2 objets chacun avec 2 chaînes. (Suite format JSON)

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

Ce que je dois faire est de combiner tous ces termes et indices dans ce qui suit, le format commandé:

terme + superscript + terme + superscript

Cependant, la Les super scripts doivent être des super scripts (d'où mon utilisation de AttributedStrings).

Répondre

2

Désolé mais pour autant que je sache, il n'y a pas de moyen facile de le faire. Vous pouvez faire quelque chose comme ce qui suit:

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, pour les 2 dernières lignes. Est-ce que cela ajoute les attributs à chaque caractère de la nouvelle chaîne attribuée? Comme, (en regardant mon exemple), le terme chaînes ne sont pas à poster en exposant, – Tyler

+0

ou garde-t-il quel caractère a quel attribut? – Tyler

+1

Il conserve juste les attributs pour chaque chaîne attribuée. Si votre AttributedString a des attributs différents sur ses différentes parties, et que vous ne savez pas où chaque partie commence/finit au préalable, vous pouvez faire un second passage sur chaque AttributedString et ajouter des attributs pour chaque caractère individuellement. Ce ne sera ni efficace ni joli, mais je ne vois pas d'autre moyen. – uoyilmaz

0

Le code ci-dessus ne fonctionne pas, parce que la méthode getAttributes() renvoie les attributs que pour le caractère courant dans l'itération Voici comment je l'ai résolu:

je l'ai fait mon propre constructeur de chaîne note que j'ajouter un espace entre les cordes

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

} 

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