Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 9eb5875

Browse files
case conversion snippets
1 parent f5ff9b7 commit 9eb5875

File tree

7 files changed

+140
-0
lines changed

7 files changed

+140
-0
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
---
2+
title: camelCase to snake_case
3+
description: Converts a camelCase string into snake_case
4+
author: Mcbencrafter
5+
tags: string,conversion,camel-case,snake-case
6+
---
7+
8+
```java
9+
public static String camelToSnake(String camelCase) {
10+
return camelCase.replaceAll("([a-z])([A-Z])", "1ドル_2ドル").toLowerCase();
11+
}
12+
13+
// Usage:
14+
System.out.println(camelToSnake("helloWorld")); // "hello_world"
15+
```
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
---
2+
title: snake_case to camelCase
3+
description: Converts a snake_case string into camelCase
4+
author: Mcbencrafter
5+
tags: string,conversion,camel-case,snake-case
6+
---
7+
8+
```java
9+
import java.util.regex.Pattern;
10+
11+
public static String snakeToCamel(String snakeCase) {
12+
return Pattern.compile("(_)([a-z])")
13+
.matcher(snakeCase)
14+
.replaceAll(match -> match.group(2).toUpperCase());
15+
}
16+
17+
// Usage:
18+
System.out.println(snakeToCamel("hello_world")); // "helloWorld"
19+
```
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
---
2+
title: String To camelCase
3+
description: Converts a string into camelCase
4+
author: Mcbencrafter
5+
tags: string,conversion,camel-case
6+
---
7+
8+
```java
9+
public static String stringToCamelCase(String text) {
10+
String[] words = text.split("\\s+");
11+
StringBuilder camelCase = new StringBuilder(
12+
words[0].substring(0, 1).toLowerCase() + words[0].substring(1)
13+
);
14+
15+
for (int i = 1; i < words.length; i++) {
16+
camelCase.append(words[i].substring(0, 1).toUpperCase());
17+
camelCase.append(words[i].substring(1));
18+
}
19+
20+
return camelCase.toString();
21+
}
22+
23+
// Usage:
24+
System.out.println(stringToCamelCase("Hello world test")); // "helloWorldTest"
25+
```
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
---
2+
title: String To param-case
3+
description: Converts a string into param-case
4+
author: Mcbencrafter
5+
tags: string,conversion,param-case
6+
---
7+
8+
```java
9+
public static String stringToParamCase(String text) {
10+
return text.toLowerCase().replaceAll("\\s+", "-");
11+
}
12+
13+
// Usage:
14+
System.out.println(stringToParamCase("Hello World 123")); // "hello-world-123"
15+
```
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
---
2+
title: String To PascalCase
3+
description: Converts a string into PascalCase
4+
author: Mcbencrafter
5+
tags: string,conversion,pascal-case
6+
---
7+
8+
```java
9+
public static String stringToPascalCase(String text) {
10+
String[] words = text.split("\\s+");
11+
StringBuilder pascalCase = new StringBuilder();
12+
13+
for (String word : words) {
14+
pascalCase.append(word.substring(0, 1).toUpperCase());
15+
pascalCase.append(word.substring(1).toLowerCase());
16+
}
17+
18+
return pascalCase.toString();
19+
}
20+
21+
// Usage:
22+
System.out.println(stringToPascalCase("hello world")); // "HelloWorld"
23+
```
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
---
2+
title: String To snake_case
3+
description: Converts a string into snake_case
4+
author: Mcbencrafter
5+
tags: string,conversion,snake-case
6+
---
7+
8+
```java
9+
public static String stringToSnakeCase(String text) {
10+
return text.toLowerCase().replaceAll("\\s+", "_");
11+
}
12+
13+
// Usage:
14+
System.out.println(stringToSnakeCase("Hello World 123")); // "hello_world_123"
15+
```
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
---
2+
title: String To Titlecase
3+
description: Converts a string into Title Case, where the first letter of each word is capitalized and the remaining letters are lowercase
4+
author: Mcbencrafter
5+
tags: string,conversion,title-case
6+
---
7+
8+
```java
9+
public static String convertToTitleCase(String text) {
10+
String[] words = text.split("(?<=\\S)(?=\\s+)|(?<=\\s+)(?=\\S)"); // this is needed to preserve spaces (text.split(" ") would remove multiple spaces)
11+
StringBuilder capitalizedText = new StringBuilder();
12+
13+
for (String word : words) {
14+
if (word.trim().isEmpty()) {
15+
capitalizedText.append(word);
16+
continue;
17+
}
18+
19+
capitalizedText.append(Character.toUpperCase(word.charAt(0)))
20+
.append(word.substring(1).toLowerCase());
21+
}
22+
23+
return capitalizedText.toString().trim();
24+
}
25+
26+
// Usage:
27+
System.out.println(convertToTitleCase("heLlo wOrld")); // "Hello World"
28+
```

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /