Compare and paint characters of a string ?

I’m trying compare and paint a value of a string. I have string obj = "COFFEE"; and I want get all characters and after paint them in yours index.

For example: C red, O yellow, F blue, E pink

How can I do this ?

I’m trying this.

GUIText wordText = new GUIText();
wordText.richText = true;
string obj = "COFFEE";


char[] w = obj.ToCharArray();
            
            for(int x = 0; x < w.Length; x++){
                if(w[x] == 'C'){                    
                   wordText.text = "<color=red>" + obj.IndexOf(w[x]) + "</color>";
                    
                }
            }

You’re on the right track but you should store your rich text in a temporary string and then set that to your label text.

GUIText wordText = new GUIText();
wordText.richText = true;
string obj = "COFFEE";
string formated_text = ""; 
 
char[] w = obj.ToCharArray();
 
for(int x = 0; x < w.Length; x++){
	if(w[x] == 'C'){                    
		formated_text += "<color=red>";
	}else if(w[x] == 'O'){ 
		formated_text += "<color=yellow>";                
    }else if
        ...
    }
     
    formated_text += obj.IndexOf(w[x]) + "</color>";  
}
wordText.text = formated_text;

Hey, I think @KayelGee was on the right lines, though I think we can make it a little more generic. The below code compares the previous character to the current one and increments an index. This index is used to lookup colour names (this is the bit I don’t like much, maybe use Color objects and convert to Hex). Then I use string.Format to create the actual marked up text substituting in the colour and character.

GUIText wordText = new GUIText();
wordText.richText = true;
string obj = "COFFEE";
string formattedText = "";
public string[] colours = new string { "red", "yellow", "blue", "pink" };
 
char[] w = obj.ToCharArray();
char previous = w[0];
int colourIndex = 0;

for(int x = 0; x < w.Length; x++)
{
    // If there's a new character get the next colour
    if(w[x] != previous)
    { 
        colourIndex++;
        if(colourIndex >= colours.Length) { colourIndex = 0; }
    }
    
    formattedText += string.Format("<color={0}>{1}</color>",
        colours[colourIndex], w[x]);
}

wordText.text = formattedText;

I hope that helps =)

Solved the problem

I did this.

public void paintCharacter(string p){
    char[] w = palavra.ToCharArray();
            char c = p[0]; 
                     
            for(int x = 0; x < w.Length; x++){
                if(w[x].Equals(c)){                    
                   string s = w[x].ToString();                   
                   palavra = palavra.Replace(s, "<color=red>" + s + "</color>");
                }                                              
            }            
            wordText.text = palavra;
}