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 7d7d077

Browse files
normalisation snippets
1 parent 872faf2 commit 7d7d077

File tree

4 files changed

+72
-0
lines changed

4 files changed

+72
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
---
2+
title: Capitalize Words
3+
description: Capitalizes the first letter of each word in a string
4+
author: Mcbencrafter
5+
tags: string,capitalize,words
6+
---
7+
8+
```java
9+
public static String capitalizeWords(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+
capitalizedText.append(Character.toUpperCase(word.charAt(0)))
19+
.append(word.substring(1));
20+
}
21+
22+
return capitalizedText.toString();
23+
}
24+
25+
// Usage:
26+
System.out.println(capitalizeWords("hello world")); // "Hello World"
27+
```
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
---
2+
title: Normalize Whitespace
3+
description: Replaces consecutive whitespaces with a single space
4+
author: Mcbencrafter
5+
tags: string,whitespace,normalize
6+
---
7+
8+
```java
9+
public static String normalizeWhitespace(String text) {
10+
return text.replaceAll(" {2,}", " ");
11+
}
12+
13+
// Usage:
14+
System.out.println(normalizeWhitespace("hello world")); // "hello world"
15+
```
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
---
2+
title: Remove Punctuation
3+
description: Removes punctuation (, . !) from a string
4+
author: Mcbencrafter
5+
tags: string,punctuation,clean,normalization
6+
---
7+
8+
```java
9+
public static String removePunctuation(String text) {
10+
return text.replaceAll("[,!.]", "");
11+
}
12+
13+
// Usage:
14+
System.out.println(removePunctuation("hello, world!")); // "hello world"
15+
```
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
---
2+
title: Remove Special Characters
3+
description: Removes any character which is not alphabetic (A-Z, a-z) or numeric (0-9)
4+
author: Mcbencrafter
5+
tags: string,special-characters,clean,normalization
6+
---
7+
8+
```java
9+
public static String removeSpecialCharacters(String text) {
10+
return text.replaceAll("[^a-zA-Z0-9]", "");
11+
}
12+
13+
// Usage:
14+
System.out.println(removeSpecialCharacters("hello, world!#%")); // "hello world"
15+
```

0 commit comments

Comments
(0)

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