 <p>For stuff like that, regular expressions are your friend:</p> <pre><code>String text = "Hello world #computerScience"; text = text.replaceAll("(#\\w+)", "ANSI_YELLOW$1ANSI_RESET"); // Hello world ANSI_YELLOW#computerScienceANSI_RESET String text = "Hello world #java #color #hashtag"; text = text.replaceAll("(#\\w+)", "ANSI_YELLOW$1ANSI_RESET"); // Hello world ANSI_YELLOW#javaANSI_RESET ANSI_YELLOW#colorANSI_RESET ANSI_YELLOW#hashtagANSI_RESET </code></pre>