ssa
ssa

Reputation: 309

How can I insert a color tag in a string? (Java)

If string is Java is Great! then how can I make it like this:

<font color="red">Java</font> <font color="blue">is</font> <font color="yellow">Great!</font>

The value of color should different for each one.

Similarly, how can I do it for all the words, e.g.

<font color="color1">J</font>
<font color="color2">a</font>
<font color="color3">v</font>
<font color="color4">a</font>
<font color="color5"> </font>
<font color="color6">i</font>
<font color="color6">s</font>
<font color="color7"> </font>
<font color="color8">G</font>
<font color="color9">r</font>
<font color="color10">e</font>
<font color="color11">a</font>
<font color="color12">t</font>
<font color="color13">!</font>

Using different String methods?

Upvotes: 1

Views: 8733

Answers (3)

Iman Marashi
Iman Marashi

Reputation: 5753

Its easy, try this code:

private final static String RED_COLOR_OPEN = "<font color=\"red\">";
private final static String RED_COLOR_CLOSE = "</font>";

and in your method :

StringBuffer sb = new StringBuffer();
    sb.append(RED_COLOR_OPEN).append(BOLD_OPEN).append(title).append(BOLD_CLOSE).append(".").append(RED_COLOR_CLOSE).trimToSize();
    sb.append(BREAK);

This will adjust the color to do the rest of your message

Upvotes: 2

MoveFast
MoveFast

Reputation: 3015

Here it goes. Essentially it is using three String functions - split() , charAt() and format() to manipulate the String.

String[] colors = {"red","blue","yellow"};
//Store the message in string
String msg = "java is great";
//Split the msg into words using String#split() method. 
//Use a regex \s+ to split the Message using space as delimiter
String[] message = msg.split("\\s+");
//Now each word in msg is available in message array.
//Iterate over each word to generate the required output format
StringBuilder s = new StringBuilder();
for(int i=0;i<message.length;i++)
{
//use the String#format() method to get the required format for String 
      s.append(String.format("<font color=\"%s\">%s</font>",
                              colors[i],message[i]);
}
String output = s.toString();

Also for the second solution. Have not included the colors array length check. Ensure that you have sufficient colors.

String[] colors = {"red","blue","yellow"};
String msg = "java is great";
StringBuilder s = new StringBuilder();
for(int i=0;i<msg.length();i++)
{
      s.append(String.format("<font color=\"%s\">%s</font>",colors[i],msg.charAt[i]);
}
String output = s.toString();

Update: Added explanation

Upvotes: 1

Vincent Ramdhanie
Vincent Ramdhanie

Reputation: 103135

To do this you need to access each character in the string one at a time. You can do that by creating a simple loop such as this:

    for(Character c: myString.toCharArray()){

    }

It may be advisable to use a StringBuilder here since you are manipulating a string. Before the loop you can create one lik ethis:

   StringBuilder sb = new StringBuilder();

To add the tag around the character in the loop you can do this:

  sb.append("<font color=\"color1\">");
  sb.append(c);
  sb.append("</font>");

After the loop you will get the new string like this:

 String result = sb.toString();

And you can do as you please with it. To get the different colors you have to decide how you are keeping those colors, say in an array, and accessing them to build the string appropriately.

Upvotes: 1

Related Questions