Reformatting a Java String
I have a string that looks like this:
CALDARI_STARSHIP_ENGINEERING
and I need to edit it to look like
Caldari Starship Engineering
Unfortunately it's three in the morning and I cannot for the life of me figure this out. I've always had trouble with replacing stuff in strings so any help would be awesome and would开发者_如何学Go help me understand how to do this in the future.
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() + "]");
// prints "[Caldari Starship Engineering]"
This split
on the word boundary anchor.
See also
- regular-expressions.info/Word boundary
Matcher
loop solution
If you don't mind using StringBuffer
, you can also use Matcher.appendReplacement/Tail
loop like this:
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() + "]");
// prints "[Caldari Starship Engineering]"
The regex uses assertion to match the "tail" part of a word, the portion that needs to be lowercased. It looks behind (?<=...)
to see that there's a word boundary \b
followed by a word character \w
. Any remaining \w+
would then need to be matched so it can be lowercased.
Related questions
- Use Java and RegEx to convert casing in a string
- Java regex does not support Perl preprocessing operations
\l
\u
,\L
, and\U
.
- Java regex does not support Perl preprocessing operations
- Java split is eating my characters.
- More examples of using assertions
- StringBuilder and StringBuffer in Java
- Unfortunately,
appendReplacement/Tail
only takesStringBuffer
- Unfortunately,
You can try this:
String originalString = "CALDARI_STARSHIP_ENGINEERING";
String newString =
WordUtils.capitalize(originalString.replace('_', ' ').toLowerCase());
WordUtils
are part of the Commons Lang libraries (http://commons.apache.org/lang/)
Using reg-exps:
String s = "CALDARI_STARSHIP_ENGINEERING";
StringBuilder camel = new StringBuilder();
Matcher m = Pattern.compile("([^_])([^_]*)").matcher(s);
while (m.find())
camel.append(m.group(1)).append(m.group(2).toLowerCase());
Untested, but thats how I implemented the same some time ago:
s = "CALDARI_STARSHIP_ENGINEERING";
StringBuilder b = new StringBuilder();
boolean upper = true;
for(char c : s.toCharArray()) {
if( upper ) {
b.append(c);
upper = false;
} else if( c = '_' ) {
b.append(" ");
upper = true;
} else {
b.append(Character.toLowerCase(c));
}
}
s = b.toString();
Please note that the EVE license agreements might forbit writing external tools that help you in your careers. And it might be the trigger for you to learn Python, because most of EVE is written in Python :).
Quick and dirty way:
Lower case all
line.toLowerCase();
Split into words:
String[] words = line.split("_");
Then loop through words capitalising first letter:
words[i].substring(0, 1).toUpperCase()
精彩评论