-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathConvertStringToCamelCase.java
More file actions
30 lines (27 loc) 路 1006 Bytes
/
Copy pathConvertStringToCamelCase.java
File metadata and controls
30 lines (27 loc) 路 1006 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
30
package main.java.kyu6;
/**
* 6 kyu - Convert string to camel case
*
* https://www.codewars.com/kata/517abf86da9663f1d2000003
*
* Details:
*
* Complete the method/function so that it converts dash/underscore delimited words into camel
* casing. The first word within the output should be capitalized only if the original word was
* capitalized (known as Upper Camel Case, also often referred to as Pascal case).
*/
public class ConvertStringToCamelCase {
public static String toCamelCase(String s) {
final String regex = "([^a-zA-Z']+)'*\\1*";
final String[] split = s.split(regex);
final StringBuilder sb = new StringBuilder();
for (int i = 0; i < split.length; i++) {
if (i == 0) {
sb.append(split[i]);
} else if (split[i].length()>0){
sb.append(split[i].replaceFirst(split[i].substring(0, 1), split[i].substring(0, 1).toUpperCase()));
}
}
return new String(sb);
}
}