Skip to content

Commit edfab6e

Browse files
Merge pull request #236 from Mcbencrafter/main
[Snippets] Added String Manipulation Snippets for java
2 parents 94cf03c + 9f019a9 commit edfab6e

31 files changed

+677
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
---
2+
title: Ascii To String
3+
description: Converts a list of ascii numbers into a string
4+
author: Mcbencrafter
5+
tags: string,ascii,encoding,decode,conversion
6+
---
7+
8+
```java
9+
import java.util.List;
10+
11+
public static String asciiToString(List<Integer> asciiCodes) {
12+
StringBuilder text = new StringBuilder();
13+
14+
for (int asciiCode : asciiCodes) {
15+
text.append((char) asciiCode);
16+
}
17+
18+
return text.toString();
19+
}
20+
21+
// Usage:
22+
System.out.println(asciiToString(List.of(104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100))); // "hello world"
23+
```
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: 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: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
---
2+
title: Check Anagram
3+
description: Checks if two strings are anagrams, meaning they contain the same characters ignoring order, spaces and case sensitivity
4+
author: Mcbencrafter
5+
tags: string,anagram,compare,arrays
6+
---
7+
8+
```java
9+
import java.util.Arrays;
10+
11+
public static boolean isAnagram(String text1, String text2) {
12+
String text1Normalized = text1.replaceAll("\\s+", "");
13+
String text2Normalized = text2.replaceAll("\\s+", "");
14+
15+
if (text1Normalized.length() != text2Normalized.length())
16+
return false;
17+
18+
char[] text1Array = text1Normalized.toCharArray();
19+
char[] text2Array = text2Normalized.toCharArray();
20+
Arrays.sort(text1Array);
21+
Arrays.sort(text2Array);
22+
return Arrays.equals(text1Array, text2Array);
23+
}
24+
25+
// Usage:
26+
System.out.println(isAnagram("listen", "silent")); // true
27+
System.out.println(isAnagram("hello", "world")); // false
28+
```
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
---
2+
title: Check Palindrome
3+
description: Checks if a string reads the same backward as forward, ignoring whitespaces and case sensitivity
4+
author: Mcbencrafter
5+
tags: string,palindrome,compare,reverse
6+
---
7+
8+
```java
9+
public static boolean isPalindrome(String text) {
10+
String cleanText = text.toLowerCase().replaceAll("\\s+", "");
11+
12+
return new StringBuilder(cleanText)
13+
.reverse()
14+
.toString()
15+
.equals(cleanText);
16+
}
17+
18+
// Usage:
19+
System.out.println(isPalindrome("A man a plan a canal Panama")); // true
20+
```
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
---
2+
title: Count Character Frequency
3+
description: Counts the frequency of each character in a string
4+
author: Mcbencrafter
5+
tags: string,character,frequency,character-frequency
6+
---
7+
8+
```java
9+
public static Map<Character, Integer> characterFrequency(String text, boolean countSpaces, boolean caseSensitive) {
10+
Map<Character, Integer> frequencyMap = new HashMap<>();
11+
12+
for (char character : text.toCharArray()) {
13+
if (character == ' ' && !countSpaces)
14+
continue;
15+
16+
if (!caseSensitive)
17+
character = Character.toLowerCase(character);
18+
19+
frequencyMap.put(character, frequencyMap.getOrDefault(character, 0) + 1);
20+
}
21+
22+
return frequencyMap;
23+
}
24+
25+
// Usage:
26+
System.out.println(characterFrequency("hello world", false, false)); // {r=1, d=1, e=1, w=1, h=1, l=3, o=2}
27+
```
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
---
2+
title: Count Character Occurrences
3+
description: Counts the occurrences of the specified characters in a given string
4+
author: Mcbencrafter
5+
tags: string,characters,counter,occurence
6+
---
7+
8+
```java
9+
import java.util.List;
10+
11+
public static int countCharacterOccurrences(String text, List<Character> characters) {
12+
int count = 0;
13+
14+
for (char character : text.toCharArray()) {
15+
if (characters.indexOf(character) == -1)
16+
continue;
17+
18+
count++;
19+
}
20+
21+
return count;
22+
}
23+
24+
// Usage:
25+
System.out.println(countCharacterOccurrences("hello world", List.of('l', 'o'))); // 5
26+
```
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
---
2+
title: Count Words
3+
description: Counts the number of words in a string
4+
author: Mcbencrafter
5+
tags: string,word,count
6+
---
7+
8+
```java
9+
public static int countWords(String text) {
10+
return text.split("\\s+").length;
11+
}
12+
13+
// Usage:
14+
System.out.println(countWords("hello world")); // 2
15+
```
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
---
2+
title: Extract Text Between Delimiters
3+
description: Extracts a text between two given delimiters from a string
4+
author: Mcbencrafter
5+
tags: string,delimiters,start,end
6+
---
7+
8+
```java
9+
public static String extractBetweenDelimiters(String text, String start, String end) {
10+
int startIndex = text.indexOf(start);
11+
int endIndex = text.indexOf(end, startIndex + start.length());
12+
13+
if (startIndex == -1 || endIndex == -1)
14+
return "";
15+
16+
return text.substring(startIndex + start.length(), endIndex);
17+
}
18+
19+
// Usage:
20+
System.out.println(extractBetweenDelimiters("hello, world!", ",", "!")); // " world"
21+
```
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
---
2+
title: Find Longest Word
3+
description: Returns the longest word in a string
4+
author: Mcbencrafter
5+
tags: string,length,words
6+
---
7+
8+
```java
9+
public static String findLongestWord(String text) {
10+
String[] words = text.split("\\s+");
11+
String longestWord = words[0];
12+
13+
for (String word : words) {
14+
if (word.length() <= longestWord.length())
15+
continue;
16+
17+
longestWord = word;
18+
}
19+
20+
return longestWord;
21+
}
22+
23+
// Usage:
24+
System.out.println(findLongestWord("hello world123")); // "world123"
25+
```

0 commit comments

Comments
 (0)