title: Rotate between colors in String
body: <p>I'm trying to create a <code>String</code> that rotates color. Every letter has a different <code>color</code>, and they need to rotate in a linear manner. The text will go inside of a <code>JLabel</code>. </p>
  
  <p>I built the text so that it could be created given a word and an array of colors (Strings).</p>
  
  <pre><code>String[] colors = {"white", "blue", "red"};
  String word = "foo";
  String coloredText;
  
  String[] letters = word.split("(?!^)"); //split text into indiv. letters
  
          coloredText = "&lt;html&gt;";
  
          for(int i = 0, j = 0; i &lt; letters.length; i++, j++){
  
              if(j &gt;= colors.length)
                  j=0;
  
              coloredText += String.format("&lt;font color='%s'&gt; %s &lt;/font&gt;", colors[j], letters[i]);
          }
  
          coloredText += "&lt;/html&gt;";
  </code></pre>
  
  <p>This will result in a string that has foo with a white <code>F</code>, blue <code>O</code> and red final <code>O</code>.</p>
  
  <p>Of course if there were less colors than letters, they would just keep on rotating.</p>
  
  <p>Now I have a <code>timer</code> to rotate the colors, but I can't figure out the <code>algorithm</code> to do it. 
  Basically, every color should take the place of the following one, with the last one replacing the first.</p>
  
  <p><strong>So for example:</strong></p>
  
  <p><a href="https://i.stack.imgur.com/xICDD.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/xICDD.png" alt="enter image description here"></a></p>
  
  <p>Of course this has to work for any number of given letters and colors, with the colors that don't fit being ignored. (A word of 3 letters with 10 colors will only use the first 3 colors).</p>
  
  <p>I have tried using <code>.replace()</code> but <code>regex</code> is a bit too difficult for me.</p>
  
  <p>Any idea?</p>