Skip to content

Instantly share code, notes, and snippets.

@mnstrspeed
Last active August 29, 2015 14:12
Show Gist options
  • Save mnstrspeed/81e2547f4d09ec67a05d to your computer and use it in GitHub Desktop.
Save mnstrspeed/81e2547f4d09ec67a05d to your computer and use it in GitHub Desktop.
CamelCase-enizer
public class CamelCase {
public static void main(String[] args) {
String line = "if at lake and lake has not been probed, blink lights, probe lake, steer away from obstacles";
System.out.println(toCamelCase(line) + "Rule");
// IfAtLakeAndLakeHasNotBeenProbedThenBlinkLightsThenProbeLakeThenSteerAwayFromObstaclesRule
}
private static String toCamelCase(String line) {
StringBuilder builder = new StringBuilder();
boolean nextUpper = true;
for (char c : line.toCharArray()) {
if (Character.isAlphabetic(c)) {
builder.append(nextUpper ? Character.toUpperCase(c) : Character.toLowerCase(c));
nextUpper = false;
} else if (c == ',') {
builder.append("Then");
nextUpper = false;
} else if (Character.isWhitespace(c)) {
nextUpper = true;
}
}
return builder.toString();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment