-
Notifications
You must be signed in to change notification settings - Fork 344
Expand file tree
/
Copy pathSnakeToCamel.java
More file actions
39 lines (34 loc) · 1.03 KB
/
SnakeToCamel.java
File metadata and controls
39 lines (34 loc) · 1.03 KB
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
31
32
33
34
35
36
37
38
39
//Converting Snake Case string to Camel Case.
//Input: str = "snake_case_to_camel_case"
//Output: "SnakeCaseToCamelCase"
import java.io.*;
class SnakeTOCamel {
// Function to convert snake case to camel case
public static String
snakeToCamel(String str)
{
// Capitalizing first letter of string
str = str.substring(0, 1).toUpperCase() + str.substring(1);
StringBuilder builder = new StringBuilder(str);
// Traversing the string character by
// character and remove underscore
// and capitalize next letter
for (int i = 0; i < builder.length(); i++) {
// Checking char is underscore
if (builder.charAt(i) == '_') {
builder.deleteCharAt(i);
builder.replace(i, i + 1,String.valueOf(Character.toUpperCase(builder.charAt(i))));
}
}
// Returning in String type
return builder.toString();
}
public static void main(String[] args){
// Given String
String str = "snake_to_camel";
// Function Call
str = snakeToCamel(str);
// Modified String
System.out.println(str);
}
}