-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAutoCamelCaser.java
More file actions
29 lines (27 loc) · 955 Bytes
/
AutoCamelCaser.java
File metadata and controls
29 lines (27 loc) · 955 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import java.lang.StringBuilder;
class Solution {
static String toCamelCase(String s) {
StringBuilder output = new StringBuilder();
char ch;
boolean capitalised = false;
if (s.length() > 0) {
String[] sArr = s.split("[-_]");
for (int i = 0; i < sArr.length; i++) {
StringBuilder temp = new StringBuilder(sArr[i]);
capitalised = false;
ch = temp.charAt(0);
if (Character.isUpperCase(ch)) {
capitalised = true;
}
if (capitalised == true || i > 0) {
ch = Character.toUpperCase(ch);
temp.setCharAt(0, ch);
output = output.append(temp);
} else {
output = output.append(temp);
}
}
}
return output.toString();
}
}