Something like this is simple enough:
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() + "]");
It is split at the anchor border of the anchor.
see also
Matcher solution loop
If you don't mind using StringBuffer , you can also use the Matcher.appendReplacement/Tail loop as follows:
String text = "CALDARI_STARSHIP_ENGINEERING"; text = text.replace("_", " "); Matcher m = Pattern.compile("(?<=\\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() + "]");
The regular expression uses the statement to match the βtailβ of the part of the word, the part that needs to be reduced. He looks behind (?<=...) to see that there is a word boundary \b followed by the character \w . Any remaining \w+ must then be matched so that they can be reduced.
Related Questions
source share