Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Something like this is simple enough:</p> <pre><code> String text = "CALDARI_STARSHIP_ENGINEERING"; text = text.replace("_", " "); StringBuilder out = new StringBuilder(); for (String s : text.split("\\b")) { if (!s.isEmpty()) { out.append(s.substring(0, 1) + s.substring(1).toLowerCase()); } } System.out.println("[" + out.toString() + "]"); // prints "[Caldari Starship Engineering]" </code></pre> <p>This <code>split</code> on the word boundary anchor.</p> <h3>See also</h3> <ul> <li><a href="http://www.regular-expressions.info/wordboundaries.html" rel="nofollow noreferrer">regular-expressions.info/Word boundary</a></li> </ul> <hr> <h3><code>Matcher</code> loop solution</h3> <p>If you don't mind using <code>StringBuffer</code>, you can also use <code>Matcher.appendReplacement/Tail</code> loop like this:</p> <pre><code> String text = "CALDARI_STARSHIP_ENGINEERING"; text = text.replace("_", " "); Matcher m = Pattern.compile("(?&lt;=\\b\\w)\\w+").matcher(text); StringBuffer sb = new StringBuffer(); while (m.find()) { m.appendReplacement(sb, m.group().toLowerCase()); } m.appendTail(sb); System.out.println("[" + sb.toString() + "]"); // prints "[Caldari Starship Engineering]" </code></pre> <p>The regex uses assertion to match the "tail" part of a word, the portion that needs to be lowercased. It looks behind <code>(?&lt;=...)</code> to see that there's a word boundary <code>\b</code> followed by a word character <code>\w</code>. Any remaining <code>\w+</code> would then need to be matched so it can be lowercased.</p> <h3>Related questions</h3> <ul> <li><a href="https://stackoverflow.com/questions/2770967/use-java-and-regex-to-convert-casing-in-a-string/">Use Java and RegEx to convert casing in a string</a> <ul> <li>Java regex does not support Perl preprocessing operations <code>\l</code> <code>\u</code>, <code>\L</code>, and <code>\U</code>. </li> </ul></li> <li><a href="https://stackoverflow.com/questions/2819933/java-split-is-eating-my-characters/">Java split is eating my characters.</a> <ul> <li>More examples of using assertions</li> </ul></li> <li><a href="https://stackoverflow.com/questions/355089/stringbuilder-and-stringbuffer-in-java">StringBuilder and StringBuffer in Java</a> <ul> <li>Unfortunately, <code>appendReplacement/Tail</code> only takes <code>StringBuffer</code></li> </ul></li> </ul>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload