From 9eb5875d27e1dbe72b411316fa47ea74deb4f41b Mon Sep 17 00:00:00 2001 From: Mcbencrafter <92606492+Mcbencrafter@users.noreply.github.com> Date: Sat, 18 Jan 2025 10:22:47 +0100 Subject: [PATCH 01/21] case conversion snippets --- .../camelcase-to-snake-case.md | 15 ++++++++++ .../snake-case-to-camelcase.md | 19 +++++++++++++ .../string-to-camelcase.md | 25 +++++++++++++++++ .../string-to-param-case.md | 15 ++++++++++ .../string-to-pascalcase.md | 23 +++++++++++++++ .../string-to-snake-case.md | 15 ++++++++++ .../string-to-titlecase.md | 28 +++++++++++++++++++ 7 files changed, 140 insertions(+) create mode 100644 snippets/java/string-manipulation/camelcase-to-snake-case.md create mode 100644 snippets/java/string-manipulation/snake-case-to-camelcase.md create mode 100644 snippets/java/string-manipulation/string-to-camelcase.md create mode 100644 snippets/java/string-manipulation/string-to-param-case.md create mode 100644 snippets/java/string-manipulation/string-to-pascalcase.md create mode 100644 snippets/java/string-manipulation/string-to-snake-case.md create mode 100644 snippets/java/string-manipulation/string-to-titlecase.md diff --git a/snippets/java/string-manipulation/camelcase-to-snake-case.md b/snippets/java/string-manipulation/camelcase-to-snake-case.md new file mode 100644 index 00000000..86219c7b --- /dev/null +++ b/snippets/java/string-manipulation/camelcase-to-snake-case.md @@ -0,0 +1,15 @@ +--- +title: camelCase to snake_case +description: Converts a camelCase string into snake_case +author: Mcbencrafter +tags: string,conversion,camel-case,snake-case +--- + +```java +public static String camelToSnake(String camelCase) { + return camelCase.replaceAll("([a-z])([A-Z])", "$1_$2").toLowerCase(); +} + +// Usage: +System.out.println(camelToSnake("helloWorld")); // "hello_world" +``` \ No newline at end of file diff --git a/snippets/java/string-manipulation/snake-case-to-camelcase.md b/snippets/java/string-manipulation/snake-case-to-camelcase.md new file mode 100644 index 00000000..fd8e3206 --- /dev/null +++ b/snippets/java/string-manipulation/snake-case-to-camelcase.md @@ -0,0 +1,19 @@ +--- +title: snake_case to camelCase +description: Converts a snake_case string into camelCase +author: Mcbencrafter +tags: string,conversion,camel-case,snake-case +--- + +```java +import java.util.regex.Pattern; + +public static String snakeToCamel(String snakeCase) { + return Pattern.compile("(_)([a-z])") + .matcher(snakeCase) + .replaceAll(match -> match.group(2).toUpperCase()); +} + +// Usage: +System.out.println(snakeToCamel("hello_world")); // "helloWorld" +``` \ No newline at end of file diff --git a/snippets/java/string-manipulation/string-to-camelcase.md b/snippets/java/string-manipulation/string-to-camelcase.md new file mode 100644 index 00000000..5a94713c --- /dev/null +++ b/snippets/java/string-manipulation/string-to-camelcase.md @@ -0,0 +1,25 @@ +--- +title: String To camelCase +description: Converts a string into camelCase +author: Mcbencrafter +tags: string,conversion,camel-case +--- + +```java +public static String stringToCamelCase(String text) { + String[] words = text.split("\\s+"); + StringBuilder camelCase = new StringBuilder( + words[0].substring(0, 1).toLowerCase() + words[0].substring(1) + ); + + for (int i = 1; i < words.length; i++) { + camelCase.append(words[i].substring(0, 1).toUpperCase()); + camelCase.append(words[i].substring(1)); + } + + return camelCase.toString(); +} + +// Usage: +System.out.println(stringToCamelCase("Hello world test")); // "helloWorldTest" +``` \ No newline at end of file diff --git a/snippets/java/string-manipulation/string-to-param-case.md b/snippets/java/string-manipulation/string-to-param-case.md new file mode 100644 index 00000000..576d3987 --- /dev/null +++ b/snippets/java/string-manipulation/string-to-param-case.md @@ -0,0 +1,15 @@ +--- +title: String To param-case +description: Converts a string into param-case +author: Mcbencrafter +tags: string,conversion,param-case +--- + +```java +public static String stringToParamCase(String text) { + return text.toLowerCase().replaceAll("\\s+", "-"); +} + +// Usage: +System.out.println(stringToParamCase("Hello World 123")); // "hello-world-123" +``` \ No newline at end of file diff --git a/snippets/java/string-manipulation/string-to-pascalcase.md b/snippets/java/string-manipulation/string-to-pascalcase.md new file mode 100644 index 00000000..f760f369 --- /dev/null +++ b/snippets/java/string-manipulation/string-to-pascalcase.md @@ -0,0 +1,23 @@ +--- +title: String To PascalCase +description: Converts a string into PascalCase +author: Mcbencrafter +tags: string,conversion,pascal-case +--- + +```java +public static String stringToPascalCase(String text) { + String[] words = text.split("\\s+"); + StringBuilder pascalCase = new StringBuilder(); + + for (String word : words) { + pascalCase.append(word.substring(0, 1).toUpperCase()); + pascalCase.append(word.substring(1).toLowerCase()); + } + + return pascalCase.toString(); +} + +// Usage: +System.out.println(stringToPascalCase("hello world")); // "HelloWorld" +``` \ No newline at end of file diff --git a/snippets/java/string-manipulation/string-to-snake-case.md b/snippets/java/string-manipulation/string-to-snake-case.md new file mode 100644 index 00000000..69a24762 --- /dev/null +++ b/snippets/java/string-manipulation/string-to-snake-case.md @@ -0,0 +1,15 @@ +--- +title: String To snake_case +description: Converts a string into snake_case +author: Mcbencrafter +tags: string,conversion,snake-case +--- + +```java +public static String stringToSnakeCase(String text) { + return text.toLowerCase().replaceAll("\\s+", "_"); +} + +// Usage: +System.out.println(stringToSnakeCase("Hello World 123")); // "hello_world_123" +``` \ No newline at end of file diff --git a/snippets/java/string-manipulation/string-to-titlecase.md b/snippets/java/string-manipulation/string-to-titlecase.md new file mode 100644 index 00000000..400fd86a --- /dev/null +++ b/snippets/java/string-manipulation/string-to-titlecase.md @@ -0,0 +1,28 @@ +--- +title: String To Titlecase +description: Converts a string into Title Case, where the first letter of each word is capitalized and the remaining letters are lowercase +author: Mcbencrafter +tags: string,conversion,title-case +--- + +```java +public static String convertToTitleCase(String text) { + String[] words = text.split("(?<=\\S)(?=\\s+)|(?<=\\s+)(?=\\S)"); // this is needed to preserve spaces (text.split(" ") would remove multiple spaces) + StringBuilder capitalizedText = new StringBuilder(); + + for (String word : words) { + if (word.trim().isEmpty()) { + capitalizedText.append(word); + continue; + } + + capitalizedText.append(Character.toUpperCase(word.charAt(0))) + .append(word.substring(1).toLowerCase()); + } + + return capitalizedText.toString().trim(); +} + +// Usage: +System.out.println(convertToTitleCase("heLlo wOrld")); // "Hello World" +``` \ No newline at end of file From 93a89b7be21777e87647aed4bc0172a3a209d97a Mon Sep 17 00:00:00 2001 From: Mcbencrafter <92606492+Mcbencrafter@users.noreply.github.com> Date: Sat, 18 Jan 2025 10:24:06 +0100 Subject: [PATCH 02/21] ascii/unicode to/from string conversion snippets --- .../string-manipulation/ascii-to-string.md | 23 ++++++++++++++++++ .../string-manipulation/string-to-ascii.md | 24 +++++++++++++++++++ .../string-manipulation/string-to-unicode.md | 21 ++++++++++++++++ .../string-manipulation/unicode-to-string.md | 23 ++++++++++++++++++ 4 files changed, 91 insertions(+) create mode 100644 snippets/java/string-manipulation/ascii-to-string.md create mode 100644 snippets/java/string-manipulation/string-to-ascii.md create mode 100644 snippets/java/string-manipulation/string-to-unicode.md create mode 100644 snippets/java/string-manipulation/unicode-to-string.md diff --git a/snippets/java/string-manipulation/ascii-to-string.md b/snippets/java/string-manipulation/ascii-to-string.md new file mode 100644 index 00000000..8e7cb924 --- /dev/null +++ b/snippets/java/string-manipulation/ascii-to-string.md @@ -0,0 +1,23 @@ +--- +title: Ascii To String +description: Converts a list of ascii numbers into a string +author: Mcbencrafter +tags: string,ascii,encoding,decode,conversion +--- + +```java +import java.util.List; + +public static String asciiToString(List asciiCodes) { + StringBuilder text = new StringBuilder(); + + for (int asciiCode : asciiCodes) { + text.append((char) asciiCode); + } + + return text.toString(); +} + +// Usage: +System.out.println(asciiToString(List.of(104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100))); // "hello world" +``` \ No newline at end of file diff --git a/snippets/java/string-manipulation/string-to-ascii.md b/snippets/java/string-manipulation/string-to-ascii.md new file mode 100644 index 00000000..d5e51458 --- /dev/null +++ b/snippets/java/string-manipulation/string-to-ascii.md @@ -0,0 +1,24 @@ +--- +title: String To Ascii +description: Converts a string into ascii numbers +author: Mcbencrafter +tags: string,ascii,encoding,conversion +--- + +```java +import java.util.ArrayList; +import java.util.List; + +public static List stringToAscii(String text) { + List asciiCodes = new ArrayList<>(); + + for (char character : text.toCharArray()) { + asciiCodes.add((int) character); + } + + return asciiCodes; +} + +// Usage: +System.out.println(stringToAscii("hello world")); // [104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100] +``` \ No newline at end of file diff --git a/snippets/java/string-manipulation/string-to-unicode.md b/snippets/java/string-manipulation/string-to-unicode.md new file mode 100644 index 00000000..cd6bc518 --- /dev/null +++ b/snippets/java/string-manipulation/string-to-unicode.md @@ -0,0 +1,21 @@ +--- +title: String To Unicode +description: Converts characters of a string into their unicode representation +author: Mcbencrafter +tags: string,unicode,encoding,conversion +--- + +```java +public static String stringToUnicode(String text) { + StringBuilder unicodeText = new StringBuilder(); + + for (char character : text.toCharArray()) { + unicodeText.append(String.format("\\u%04x", (int) character)); + } + + return unicodeText.toString(); +} + +// Usage: +System.out.println(stringToUnicode("hello world")); // \u0068\u0065\u006C\u006C\u006F\u0020\u0077\u006F\u0072\u006C\u0064 +``` \ No newline at end of file diff --git a/snippets/java/string-manipulation/unicode-to-string.md b/snippets/java/string-manipulation/unicode-to-string.md new file mode 100644 index 00000000..4846c823 --- /dev/null +++ b/snippets/java/string-manipulation/unicode-to-string.md @@ -0,0 +1,23 @@ +--- +title: Unicode To String +description: Converts a unicode String into its normal representation +author: Mcbencrafter +tags: string,unicode,encoding,decoding,conversion +--- + +```java +public static String unicodeToString(String unicode) { + StringBuilder string = new StringBuilder(); + String[] hex = unicode.split("\\\\u"); + + for (int symbol = 1; symbol < hex.length; symbol++) { + int data = Integer.parseInt(hex[symbol], 16); + string.append((char) data); + } + + return string.toString(); +} + +// Usage: +System.out.println(unicodeToString("\\u0068\\u0065\\u006c\\u006c\\u006f\\u0020\\u0077\\u006f\\u0072\\u006c\\u0064")); // "hello world" +``` \ No newline at end of file From d532525161bd00e6c0d02887f6703cf9bcf52e14 Mon Sep 17 00:00:00 2001 From: Mcbencrafter <92606492+Mcbencrafter@users.noreply.github.com> Date: Sat, 18 Jan 2025 10:24:39 +0100 Subject: [PATCH 03/21] tab/space conversion snippets --- .../java/string-manipulation/spaces-to-tabs.md | 15 +++++++++++++++ .../java/string-manipulation/tabs-to-spaces.md | 15 +++++++++++++++ 2 files changed, 30 insertions(+) create mode 100644 snippets/java/string-manipulation/spaces-to-tabs.md create mode 100644 snippets/java/string-manipulation/tabs-to-spaces.md diff --git a/snippets/java/string-manipulation/spaces-to-tabs.md b/snippets/java/string-manipulation/spaces-to-tabs.md new file mode 100644 index 00000000..197aa2c7 --- /dev/null +++ b/snippets/java/string-manipulation/spaces-to-tabs.md @@ -0,0 +1,15 @@ +--- +Title: Spaces To Tabs +description: Converts spaces into tabs +author: Mcbencrafter +tags: string,tab,space,conversion +--- + +```java +public static String convertTabToSpace(String text, int spacesPerTab) { + return text.replaceAll("\t", " ".repeat(spacesPerTab)); +} + +// Usage: +System.out.println(convertTabToSpace("hello\tworld", 2)); // "hello world" +``` \ No newline at end of file diff --git a/snippets/java/string-manipulation/tabs-to-spaces.md b/snippets/java/string-manipulation/tabs-to-spaces.md new file mode 100644 index 00000000..23b7f18d --- /dev/null +++ b/snippets/java/string-manipulation/tabs-to-spaces.md @@ -0,0 +1,15 @@ +--- +Title: Tabs To Spaces +description: Converts tabs into spaces +author: Mcbencrafter +tags: string,tab,space,conversion +--- + +```java +public static String convertTabToSpace(String text, int spacesPerTab) { + return text.replaceAll("\t", " ".repeat(spacesPerTab)); +} + +// Usage: +System.out.println(convertTabToSpace("hello\tworld", 2)); // "hello world" +``` \ No newline at end of file From aa6683b15a37bad7bb4da31e3034005fb79a2eac Mon Sep 17 00:00:00 2001 From: Mcbencrafter <92606492+Mcbencrafter@users.noreply.github.com> Date: Sat, 18 Jan 2025 10:25:00 +0100 Subject: [PATCH 04/21] palindrome/anagram check snippets --- .../java/string-manipulation/check-anagram.md | 28 +++++++++++++++++++ .../string-manipulation/check-palindrome.md | 20 +++++++++++++ 2 files changed, 48 insertions(+) create mode 100644 snippets/java/string-manipulation/check-anagram.md create mode 100644 snippets/java/string-manipulation/check-palindrome.md diff --git a/snippets/java/string-manipulation/check-anagram.md b/snippets/java/string-manipulation/check-anagram.md new file mode 100644 index 00000000..f1d27dee --- /dev/null +++ b/snippets/java/string-manipulation/check-anagram.md @@ -0,0 +1,28 @@ +--- +title: Check Anagram +description: Checks if two strings are anagrams, meaning they contain the same characters ignoring order, spaces and case sensitivity +author: Mcbencrafter +tags: string,anagram,compare,arrays +--- + +```java +import java.util.Arrays; + +public static boolean isAnagram(String text1, String text2) { + String text1Normalized = text1.replaceAll("\\s+", ""); + String text2Normalized = text2.replaceAll("\\s+", ""); + + if (text1Normalized.length() != text2Normalized.length()) + return false; + + char[] text1Array = text1Normalized.toCharArray(); + char[] text2Array = text2Normalized.toCharArray(); + Arrays.sort(text1Array); + Arrays.sort(text2Array); + return Arrays.equals(text1Array, text2Array); +} + +// Usage: +System.out.println(isAnagram("listen", "silent")); // true +System.out.println(isAnagram("hello", "world")); // false +``` \ No newline at end of file diff --git a/snippets/java/string-manipulation/check-palindrome.md b/snippets/java/string-manipulation/check-palindrome.md new file mode 100644 index 00000000..dde8ac34 --- /dev/null +++ b/snippets/java/string-manipulation/check-palindrome.md @@ -0,0 +1,20 @@ +--- +title: Check Palindrome +description: Checks if a string reads the same backward as forward, ignoring whitespaces and case sensitivity +author: Mcbencrafter +tags: string,palindrome,compare,reverse +--- + +```java +public static boolean isPalindrome(String text) { + String cleanText = text.toLowerCase().replaceAll("\\s+", ""); + + return new StringBuilder(cleanText) + .reverse() + .toString() + .equals(cleanText); +} + +// Usage: +System.out.println(isPalindrome("A man a plan a canal Panama")); // true +``` \ No newline at end of file From 872faf2b5f3191d658da17189e8c1bee48f12acb Mon Sep 17 00:00:00 2001 From: Mcbencrafter <92606492+Mcbencrafter@users.noreply.github.com> Date: Sat, 18 Jan 2025 10:25:32 +0100 Subject: [PATCH 05/21] counting snippets --- .../count-character-frequency.md | 27 +++++++++++++++++++ .../string-manipulation/count-consonants.md | 25 +++++++++++++++++ .../java/string-manipulation/count-vowels.md | 25 +++++++++++++++++ .../java/string-manipulation/count-words.md | 15 +++++++++++ 4 files changed, 92 insertions(+) create mode 100644 snippets/java/string-manipulation/count-character-frequency.md create mode 100644 snippets/java/string-manipulation/count-consonants.md create mode 100644 snippets/java/string-manipulation/count-vowels.md create mode 100644 snippets/java/string-manipulation/count-words.md diff --git a/snippets/java/string-manipulation/count-character-frequency.md b/snippets/java/string-manipulation/count-character-frequency.md new file mode 100644 index 00000000..85082d64 --- /dev/null +++ b/snippets/java/string-manipulation/count-character-frequency.md @@ -0,0 +1,27 @@ +--- +title: Count Character Frequency +description: Counts the frequency of each character in a string +author: Mcbencrafter +tags: string,character,frequency,character-frequency +--- + +```java +public static Map characterFrequency(String text, boolean countSpaces, boolean caseSensitive) { + Map frequencyMap = new HashMap<>(); + + for (char character : text.toCharArray()) { + if (character == ' ' && !countSpaces) + continue; + + if (!caseSensitive) + character = Character.toLowerCase(character); + + frequencyMap.put(character, frequencyMap.getOrDefault(character, 0) + 1); + } + + return frequencyMap; +} + +// Usage: +System.out.println(characterFrequency("hello world", false, false)); // {r=1, d=1, e=1, w=1, h=1, l=3, o=2} +``` \ No newline at end of file diff --git a/snippets/java/string-manipulation/count-consonants.md b/snippets/java/string-manipulation/count-consonants.md new file mode 100644 index 00000000..cd97dbe1 --- /dev/null +++ b/snippets/java/string-manipulation/count-consonants.md @@ -0,0 +1,25 @@ +--- +title: Count Consonants +description: Counts the consonants (every character except for a, e, i, o, u) in a string +author: Mcbencrafter +tags: string,consonants,counter +--- + +```java +public static int countConsonants(String text) { + String consonants = "bcdfghjklmnpqrstvwxyz"; + int count = 0; + + for (char character : text.toLowerCase().toCharArray()) { + if (consonants.indexOf(character) == -1) + continue; + + count++; + } + + return count; +} + +// Usage: +System.out.println(countConsonants("hello world")); // 7 +``` \ No newline at end of file diff --git a/snippets/java/string-manipulation/count-vowels.md b/snippets/java/string-manipulation/count-vowels.md new file mode 100644 index 00000000..e7bb3370 --- /dev/null +++ b/snippets/java/string-manipulation/count-vowels.md @@ -0,0 +1,25 @@ +--- +title: Count Vowels +description: Counts the vowels (a, e, i, o, u) in a string +author: Mcbencrafter +tags: string,vowels,counter +--- + +```java +public static int countVowels(String text) { + String vowels = "aeiou"; + int count = 0; + + for (char character : text.toLowerCase().toCharArray()) { + if (vowels.indexOf(character) == -1) + continue; + + count++; + } + + return count; +} + +// Usage: +System.out.println(countVowels("hello world")); // 3 +``` \ No newline at end of file diff --git a/snippets/java/string-manipulation/count-words.md b/snippets/java/string-manipulation/count-words.md new file mode 100644 index 00000000..b0293ef1 --- /dev/null +++ b/snippets/java/string-manipulation/count-words.md @@ -0,0 +1,15 @@ +--- +title: Count Words +description: Counts the number of words in a string +author: Mcbencrafter +tags: string,word,count +--- + +```java +public static int countWords(String text) { + return text.split("\\s+").length; +} + +// Usage: +System.out.println(countWords("hello world")); // 2 +``` \ No newline at end of file From 7d7d077ac2664bf4554b27144834a2eb34d153fd Mon Sep 17 00:00:00 2001 From: Mcbencrafter <92606492+Mcbencrafter@users.noreply.github.com> Date: Sat, 18 Jan 2025 10:26:21 +0100 Subject: [PATCH 06/21] normalisation snippets --- .../string-manipulation/capitalize-words.md | 27 +++++++++++++++++++ .../normalize-whitespace.md | 15 +++++++++++ .../string-manipulation/remove-punctuation.md | 15 +++++++++++ .../remove-special-characters.md | 15 +++++++++++ 4 files changed, 72 insertions(+) create mode 100644 snippets/java/string-manipulation/capitalize-words.md create mode 100644 snippets/java/string-manipulation/normalize-whitespace.md create mode 100644 snippets/java/string-manipulation/remove-punctuation.md create mode 100644 snippets/java/string-manipulation/remove-special-characters.md diff --git a/snippets/java/string-manipulation/capitalize-words.md b/snippets/java/string-manipulation/capitalize-words.md new file mode 100644 index 00000000..81327166 --- /dev/null +++ b/snippets/java/string-manipulation/capitalize-words.md @@ -0,0 +1,27 @@ +--- +title: Capitalize Words +description: Capitalizes the first letter of each word in a string +author: Mcbencrafter +tags: string,capitalize,words +--- + +```java +public static String capitalizeWords(String text) { + String[] words = text.split("(?<=\\S)(?=\\s+)|(?<=\\s+)(?=\\S)"); // this is needed to preserve spaces (text.split(" ") would remove multiple spaces) + StringBuilder capitalizedText = new StringBuilder(); + + for (String word : words) { + if (word.trim().isEmpty()) { + capitalizedText.append(word); + continue; + } + capitalizedText.append(Character.toUpperCase(word.charAt(0))) + .append(word.substring(1)); + } + + return capitalizedText.toString(); +} + +// Usage: +System.out.println(capitalizeWords("hello world")); // "Hello World" +``` \ No newline at end of file diff --git a/snippets/java/string-manipulation/normalize-whitespace.md b/snippets/java/string-manipulation/normalize-whitespace.md new file mode 100644 index 00000000..15c0df7a --- /dev/null +++ b/snippets/java/string-manipulation/normalize-whitespace.md @@ -0,0 +1,15 @@ +--- +title: Normalize Whitespace +description: Replaces consecutive whitespaces with a single space +author: Mcbencrafter +tags: string,whitespace,normalize +--- + +```java +public static String normalizeWhitespace(String text) { + return text.replaceAll(" {2,}", " "); +} + +// Usage: +System.out.println(normalizeWhitespace("hello world")); // "hello world" +``` \ No newline at end of file diff --git a/snippets/java/string-manipulation/remove-punctuation.md b/snippets/java/string-manipulation/remove-punctuation.md new file mode 100644 index 00000000..d144e3a4 --- /dev/null +++ b/snippets/java/string-manipulation/remove-punctuation.md @@ -0,0 +1,15 @@ +--- +title: Remove Punctuation +description: Removes punctuation (, . !) from a string +author: Mcbencrafter +tags: string,punctuation,clean,normalization +--- + +```java +public static String removePunctuation(String text) { + return text.replaceAll("[,!.]", ""); +} + +// Usage: +System.out.println(removePunctuation("hello, world!")); // "hello world" +``` \ No newline at end of file diff --git a/snippets/java/string-manipulation/remove-special-characters.md b/snippets/java/string-manipulation/remove-special-characters.md new file mode 100644 index 00000000..a784478b --- /dev/null +++ b/snippets/java/string-manipulation/remove-special-characters.md @@ -0,0 +1,15 @@ +--- +title: Remove Special Characters +description: Removes any character which is not alphabetic (A-Z, a-z) or numeric (0-9) +author: Mcbencrafter +tags: string,special-characters,clean,normalization +--- + +```java +public static String removeSpecialCharacters(String text) { + return text.replaceAll("[^a-zA-Z0-9]", ""); +} + +// Usage: +System.out.println(removeSpecialCharacters("hello, world!#%")); // "hello world" +``` \ No newline at end of file From aa4cd5f83eb22962c77488de24bdb4183cecc628 Mon Sep 17 00:00:00 2001 From: Mcbencrafter <92606492+Mcbencrafter@users.noreply.github.com> Date: Sat, 18 Jan 2025 10:26:38 +0100 Subject: [PATCH 07/21] reverse snippets --- .../reverse-word-contents.md | 23 +++++++++++++++++++ .../reverse-words-in-string.md | 22 ++++++++++++++++++ 2 files changed, 45 insertions(+) create mode 100644 snippets/java/string-manipulation/reverse-word-contents.md create mode 100644 snippets/java/string-manipulation/reverse-words-in-string.md diff --git a/snippets/java/string-manipulation/reverse-word-contents.md b/snippets/java/string-manipulation/reverse-word-contents.md new file mode 100644 index 00000000..8112f768 --- /dev/null +++ b/snippets/java/string-manipulation/reverse-word-contents.md @@ -0,0 +1,23 @@ +--- +Title: Reverse Word Contents +Description: Reverses the characters of each word in a string while preserving word order +Author: Mcbencrafter +Tags: string,reverse,words,transformation,order +--- + +```java +public static String reverseWords(String text) { + String[] words = text.split("\\s+"); + StringBuilder reversedText = new StringBuilder(); + + for (String word : words) { + StringBuilder reversedWord = new StringBuilder(word).reverse(); + reversedText.append(reversedWord).append(" "); + } + + return reversedText.toString().trim(); +} + +// Usage: +System.out.println(reverseWordContents("hello world")); // "olleh dlrow" +``` \ No newline at end of file diff --git a/snippets/java/string-manipulation/reverse-words-in-string.md b/snippets/java/string-manipulation/reverse-words-in-string.md new file mode 100644 index 00000000..8ef1752f --- /dev/null +++ b/snippets/java/string-manipulation/reverse-words-in-string.md @@ -0,0 +1,22 @@ +--- +Title: Reverse Words In String +Description: Reverses the order of words in a sentence while preserving the content of each word +Author: Mcbencrafter +Tags: string,reverse,words,transformation,sentence +--- + +```java +public static String reverseWords(String text) { + String[] words = text.split("\\s+"); + StringBuilder reversedSentence = new StringBuilder(); + + for (int currentWord = words.length - 1; currentWord >= 0; currentWord--) { + reversedSentence.append(words[currentWord]).append(" "); + } + + return reversedSentence.toString().trim(); +} + +// Usage: +System.out.println(reverseWords("hello world")); // Output: world hello +``` \ No newline at end of file From 7832e6092ead818a0e10a59b8f58cc177bfbe3f6 Mon Sep 17 00:00:00 2001 From: Mcbencrafter <92606492+Mcbencrafter@users.noreply.github.com> Date: Sat, 18 Jan 2025 10:27:14 +0100 Subject: [PATCH 08/21] hide/truncate snippets --- .../java/string-manipulation/mask-text.md | 25 +++++++++++++++++++ .../string-manipulation/truncate-string.md | 18 +++++++++++++ 2 files changed, 43 insertions(+) create mode 100644 snippets/java/string-manipulation/mask-text.md create mode 100644 snippets/java/string-manipulation/truncate-string.md diff --git a/snippets/java/string-manipulation/mask-text.md b/snippets/java/string-manipulation/mask-text.md new file mode 100644 index 00000000..c7f51314 --- /dev/null +++ b/snippets/java/string-manipulation/mask-text.md @@ -0,0 +1,25 @@ +--- +title: Mask Text +description: Masks portions of a string, leaving specific parts at the beginning and end visible while replacing the rest with a specified character +author: Mcbencrafter +tags: string,mask,hide +--- + +```java +public static String partialMask(String text, int maskLengthStart, int maskLengthEnd, char mask) + if (text == null) + return null; + + StringBuilder maskedText = new StringBuilder(); + maskedText.append(text, 0, maskLengthStart); + + for (int currentChar = maskLengthStart; currentChar < text.length(); currentChar++) { + maskedText.append(mask); + } + maskedText.append(text, text.length() - maskLengthEnd, text.length()); + return maskedText.toString(); +} + +// Usage: +System.out.println(partialMask("1234567890", 4, 2, '*')); // "1234****90" +``` \ No newline at end of file diff --git a/snippets/java/string-manipulation/truncate-string.md b/snippets/java/string-manipulation/truncate-string.md new file mode 100644 index 00000000..6401e867 --- /dev/null +++ b/snippets/java/string-manipulation/truncate-string.md @@ -0,0 +1,18 @@ +--- +title: Truncate String +description: Truncates a string after a specified length (can also be used for hiding information) +author: Mcbencrafter +tags: string,truncate,mask,hide +--- + +```java +public static String truncate(String text, int length, String suffix) { + if (text.length() <= length) + return text; + + return text.substring(0, length).trim() + (suffix != null ? suffix : ""); +} + +// Usage: +System.out.println(truncate("hello world", 5, "...")); // "hello..." +``` \ No newline at end of file From f5b07b50a1d5c2c7c74725fab7f4c27e3a0e4cb3 Mon Sep 17 00:00:00 2001 From: Mcbencrafter <92606492+Mcbencrafter@users.noreply.github.com> Date: Sat, 18 Jan 2025 10:28:06 +0100 Subject: [PATCH 09/21] slugify snippet --- .../string-manipulation/slugify-string.md | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 snippets/java/string-manipulation/slugify-string.md diff --git a/snippets/java/string-manipulation/slugify-string.md b/snippets/java/string-manipulation/slugify-string.md new file mode 100644 index 00000000..b12b2432 --- /dev/null +++ b/snippets/java/string-manipulation/slugify-string.md @@ -0,0 +1,31 @@ +--- +title: Slugify String +description: Converts a string into a URL-friendly slug format +author: Mcbencrafter +tags: string,slug,slugify +--- + +```java +public static String slugify(String text, String separator) { + if (text == null) + return ""; + + // used to decompose accented characters to their base characters (e.g. "é" to "e") + String normalizedString = Normalizer.normalize(text, Normalizer.Form.NFD); + normalizedString = normalizedString.replaceAll("[\\p{InCombiningDiacriticalMarks}]", ""); + + String slug = normalizedString.trim() + .toLowerCase() + .replaceAll("\\s+", separator) + .replaceAll("[^a-z0-9\\-_" + separator + "]", "") + .replaceAll("_", separator) + .replaceAll("-", separator) + .replaceAll(separator + "+", separator) + .replaceAll(separator + "$", ""); + + return slug; +} + +// Usage: +System.out.println(slugify("Hello World-#123-é", "-")); // "hello-world-123-e" +``` \ No newline at end of file From dee9622a9f84893a84e77f44db0d3cffd91b099f Mon Sep 17 00:00:00 2001 From: Mcbencrafter <92606492+Mcbencrafter@users.noreply.github.com> Date: Sat, 18 Jan 2025 10:28:20 +0100 Subject: [PATCH 10/21] password generator snippet --- .../string-manipulation/password-generator.md | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 snippets/java/string-manipulation/password-generator.md diff --git a/snippets/java/string-manipulation/password-generator.md b/snippets/java/string-manipulation/password-generator.md new file mode 100644 index 00000000..5e0add08 --- /dev/null +++ b/snippets/java/string-manipulation/password-generator.md @@ -0,0 +1,38 @@ +--- +title: Password Generator +description: Generates a random string with specified length and character set, including options for letters, numbers, and special characters +author: Mcbencrafter +tags: string,password,generator,security,random,token +--- + +```java +public static String randomString(int length, boolean useLetters, boolean useNumbers, boolean useSpecialCharacters) { + String characters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; + String numbers = "0123456789"; + String specialCharacters = "!@#$%^&*()_+-=[]{}|;:,.<>?"; + + String allowedCharacters = ""; + + if (useLetters) + allowedCharacters += characters; + + if (useNumbers) + allowedCharacters += numbers; + + if (useSpecialCharacters) + allowedCharacters += specialCharacters; + + SecureRandom random = new SecureRandom(); + StringBuilder result = new StringBuilder(length); + + for (int i = 0; i < length; i++) { + int index = random.nextInt(allowedCharacters.length()); + result.append(allowedCharacters.charAt(index)); + } + + return result.toString(); +} + +// Usage: +System.out.println(randomString(10, true, true, false)); // Random string containing letters, numbers but no special characters with 10 characters +``` \ No newline at end of file From 0e1bf3a58ad757df4e3287e514c1e71a2b2aca18 Mon Sep 17 00:00:00 2001 From: Mcbencrafter <92606492+Mcbencrafter@users.noreply.github.com> Date: Sat, 18 Jan 2025 10:29:09 +0100 Subject: [PATCH 11/21] text between delimiters extraction snippet --- .../extract-text-between-delimiters.md | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 snippets/java/string-manipulation/extract-text-between-delimiters.md diff --git a/snippets/java/string-manipulation/extract-text-between-delimiters.md b/snippets/java/string-manipulation/extract-text-between-delimiters.md new file mode 100644 index 00000000..cededfb6 --- /dev/null +++ b/snippets/java/string-manipulation/extract-text-between-delimiters.md @@ -0,0 +1,21 @@ +--- +title: Extract Text Between Delimiters +description: Extracts a text between two given delimiters from a string +author: Mcbencrafter +tags: string,delimiters,start,end +--- + +```java +public static String extractBetweenDelimiters(String text, String start, String end) { + int startIndex = text.indexOf(start); + int endIndex = text.indexOf(end, startIndex + start.length()); + + if (startIndex == -1 || endIndex == -1) + return ""; + + return text.substring(startIndex + start.length(), endIndex); +} + +// Usage: +System.out.println(extractBetweenDelimiters("hello, world!", ",", "!")); // " world" +``` \ No newline at end of file From ea96f635a1cdb479a11daa29cdc4100a94446e88 Mon Sep 17 00:00:00 2001 From: Mcbencrafter <92606492+Mcbencrafter@users.noreply.github.com> Date: Sat, 18 Jan 2025 10:29:32 +0100 Subject: [PATCH 12/21] finding snippets --- .../string-manipulation/find-longest-word.md | 25 +++++++++++++++++++ .../find-unique-characters.md | 25 +++++++++++++++++++ 2 files changed, 50 insertions(+) create mode 100644 snippets/java/string-manipulation/find-longest-word.md create mode 100644 snippets/java/string-manipulation/find-unique-characters.md diff --git a/snippets/java/string-manipulation/find-longest-word.md b/snippets/java/string-manipulation/find-longest-word.md new file mode 100644 index 00000000..4279c960 --- /dev/null +++ b/snippets/java/string-manipulation/find-longest-word.md @@ -0,0 +1,25 @@ +--- +title: Find Longest Word +description: Returns the longest word in a string +author: Mcbencrafter +tags: string,length,words +--- + +```java +public static String findLongestWord(String text) { + String[] words = text.split("\\s+"); + String longestWord = words[0]; + + for (String word : words) { + if (word.length() <= longestWord.length()) + continue; + + longestWord = word; + } + + return longestWord; +} + +// Usage: +System.out.println(findLongestWord("hello world123")); // "world123" +``` \ No newline at end of file diff --git a/snippets/java/string-manipulation/find-unique-characters.md b/snippets/java/string-manipulation/find-unique-characters.md new file mode 100644 index 00000000..b52d91e6 --- /dev/null +++ b/snippets/java/string-manipulation/find-unique-characters.md @@ -0,0 +1,25 @@ +--- +Title: Find Unique Characters +Description: Returns a set of unique characters from a string, with options to include spaces and control case sensitivity +Author: Mcbencrafter +Tags: string,unique,characters,case-sensitive +--- + +```java +public static Set findUniqueCharacters(String text, boolean countSpaces, boolean caseSensitive) { + Set uniqueCharacters = new TreeSet<>(); + + for (char character : text.toCharArray()) { + if (character == ' ' && !countSpaces) + continue; + if (!caseSensitive) + character = Character.toLowerCase(character); + uniqueCharacters.add(character); + } + + return uniqueCharacters; +} + +// Usage: +System.out.println(findUniqueCharacters("hello world", false, true)); // Output: [d, e, h, l, o, r, w] +``` \ No newline at end of file From 51a63b10bd22f80952a9ff9824519a1b4f9d92d1 Mon Sep 17 00:00:00 2001 From: Mcbencrafter <92606492+Mcbencrafter@users.noreply.github.com> Date: Sat, 18 Jan 2025 11:03:24 +0100 Subject: [PATCH 13/21] renamed snippet for clarity --- .../{reverse-words-in-string.md => reverse-word-order.md} | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename snippets/java/string-manipulation/{reverse-words-in-string.md => reverse-word-order.md} (92%) diff --git a/snippets/java/string-manipulation/reverse-words-in-string.md b/snippets/java/string-manipulation/reverse-word-order.md similarity index 92% rename from snippets/java/string-manipulation/reverse-words-in-string.md rename to snippets/java/string-manipulation/reverse-word-order.md index 8ef1752f..7e18b6de 100644 --- a/snippets/java/string-manipulation/reverse-words-in-string.md +++ b/snippets/java/string-manipulation/reverse-word-order.md @@ -1,5 +1,5 @@ --- -Title: Reverse Words In String +Title: Reverse Word Order Description: Reverses the order of words in a sentence while preserving the content of each word Author: Mcbencrafter Tags: string,reverse,words,transformation,sentence From f330c0241bbd49794fde32f82fca1f321f7b709c Mon Sep 17 00:00:00 2001 From: Mcbencrafter <92606492+Mcbencrafter@users.noreply.github.com> Date: Sat, 18 Jan 2025 22:03:20 +0100 Subject: [PATCH 14/21] corrected spaces to tabs snippet --- .husky/pre-commit | 3 --- snippets/java/string-manipulation/spaces-to-tabs.md | 6 +++--- 2 files changed, 3 insertions(+), 6 deletions(-) delete mode 100644 .husky/pre-commit diff --git a/.husky/pre-commit b/.husky/pre-commit deleted file mode 100644 index 0a42c304..00000000 --- a/.husky/pre-commit +++ /dev/null @@ -1,3 +0,0 @@ -npm run cspell -npm run lint -npm run build diff --git a/snippets/java/string-manipulation/spaces-to-tabs.md b/snippets/java/string-manipulation/spaces-to-tabs.md index 197aa2c7..91300110 100644 --- a/snippets/java/string-manipulation/spaces-to-tabs.md +++ b/snippets/java/string-manipulation/spaces-to-tabs.md @@ -6,10 +6,10 @@ tags: string,tab,space,conversion --- ```java -public static String convertTabToSpace(String text, int spacesPerTab) { - return text.replaceAll("\t", " ".repeat(spacesPerTab)); +public static String convertSpacesToTab(String text, int spacesPerTab) { + return text.replaceAll(" ".repeat(spacesPerTab), "\t"); } // Usage: -System.out.println(convertTabToSpace("hello\tworld", 2)); // "hello world" +System.out.println(convertSpacesToTab("hello world", 4)); // Output: hello\tworld ``` \ No newline at end of file From 648c6bf46189f80742a3f37d9d38ae6b830272fd Mon Sep 17 00:00:00 2001 From: Mcbencrafter <92606492+Mcbencrafter@users.noreply.github.com> Date: Sat, 18 Jan 2025 22:22:07 +0100 Subject: [PATCH 15/21] added accidentally removed file --- .husky/pre-commit | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 .husky/pre-commit diff --git a/.husky/pre-commit b/.husky/pre-commit new file mode 100644 index 00000000..89704d20 --- /dev/null +++ b/.husky/pre-commit @@ -0,0 +1,3 @@ +npm run cspell +npm run lint +npm run build \ No newline at end of file From 41c040487078e9761cb7605a16d4b39a8c2a2d95 Mon Sep 17 00:00:00 2001 From: Mcbencrafter <92606492+Mcbencrafter@users.noreply.github.com> Date: Sat, 18 Jan 2025 22:24:58 +0100 Subject: [PATCH 16/21] tried to fix accidentally removed pre-commit file --- .husky/pre-commit | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.husky/pre-commit b/.husky/pre-commit index 89704d20..dd8ad688 100644 --- a/.husky/pre-commit +++ b/.husky/pre-commit @@ -1,3 +1,5 @@ +npm run snippets:check npm run cspell npm run lint +npm run test-ci npm run build \ No newline at end of file From abd118f7ee528e1c4e63c39532cd1429023f0b13 Mon Sep 17 00:00:00 2001 From: Mcbencrafter <92606492+Mcbencrafter@users.noreply.github.com> Date: Sat, 18 Jan 2025 22:41:16 +0100 Subject: [PATCH 17/21] tried to revert pre commit file --- .husky/pre-commit | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/.husky/pre-commit b/.husky/pre-commit index dd8ad688..3d3ab8e7 100644 --- a/.husky/pre-commit +++ b/.husky/pre-commit @@ -1,5 +1,3 @@ -npm run snippets:check -npm run cspell -npm run lint -npm run test-ci +npm run cspell +npm run lint npm run build \ No newline at end of file From 31ec9f847ce5cf7a5ae54cbc2b303b9084bc32f6 Mon Sep 17 00:00:00 2001 From: Mcbencrafter <92606492+Mcbencrafter@users.noreply.github.com> Date: Sat, 18 Jan 2025 22:44:41 +0100 Subject: [PATCH 18/21] Tried to restore pre-commit file --- .husky/pre-commit | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/.husky/pre-commit b/.husky/pre-commit index 3d3ab8e7..dd8ad688 100644 --- a/.husky/pre-commit +++ b/.husky/pre-commit @@ -1,3 +1,5 @@ -npm run cspell -npm run lint +npm run snippets:check +npm run cspell +npm run lint +npm run test-ci npm run build \ No newline at end of file From e15fc2a3efbed5ca317cef79a6a3dae6f5d5cbd9 Mon Sep 17 00:00:00 2001 From: Mcbencrafter <92606492+Mcbencrafter@users.noreply.github.com> Date: Sun, 19 Jan 2025 10:30:41 +0100 Subject: [PATCH 19/21] replaced count consonants/vowels with a more generic snippet --- .../count-character-occurrences.md | 26 +++++++++++++++++++ .../string-manipulation/count-consonants.md | 25 ------------------ .../java/string-manipulation/count-vowels.md | 25 ------------------ 3 files changed, 26 insertions(+), 50 deletions(-) create mode 100644 snippets/java/string-manipulation/count-character-occurrences.md delete mode 100644 snippets/java/string-manipulation/count-consonants.md delete mode 100644 snippets/java/string-manipulation/count-vowels.md diff --git a/snippets/java/string-manipulation/count-character-occurrences.md b/snippets/java/string-manipulation/count-character-occurrences.md new file mode 100644 index 00000000..6906fe14 --- /dev/null +++ b/snippets/java/string-manipulation/count-character-occurrences.md @@ -0,0 +1,26 @@ +--- +title: Count Character Occurrences +description: Counts the occurrences of the specified characters in a given string +author: Mcbencrafter +tags: string,characters,counter,occurence +--- + +```java +import java.util.List; + +public static int countCharacterOccurrences(String text, List characters) { + int count = 0; + + for (char character : text.toCharArray()) { + if (characters.indexOf(character) == -1) + continue; + + count++; + } + + return count; +} + +// Usage: +System.out.println(countCharacterOccurrences("hello world", List.of('l', 'o'))); // 5 +``` \ No newline at end of file diff --git a/snippets/java/string-manipulation/count-consonants.md b/snippets/java/string-manipulation/count-consonants.md deleted file mode 100644 index cd97dbe1..00000000 --- a/snippets/java/string-manipulation/count-consonants.md +++ /dev/null @@ -1,25 +0,0 @@ ---- -title: Count Consonants -description: Counts the consonants (every character except for a, e, i, o, u) in a string -author: Mcbencrafter -tags: string,consonants,counter ---- - -```java -public static int countConsonants(String text) { - String consonants = "bcdfghjklmnpqrstvwxyz"; - int count = 0; - - for (char character : text.toLowerCase().toCharArray()) { - if (consonants.indexOf(character) == -1) - continue; - - count++; - } - - return count; -} - -// Usage: -System.out.println(countConsonants("hello world")); // 7 -``` \ No newline at end of file diff --git a/snippets/java/string-manipulation/count-vowels.md b/snippets/java/string-manipulation/count-vowels.md deleted file mode 100644 index e7bb3370..00000000 --- a/snippets/java/string-manipulation/count-vowels.md +++ /dev/null @@ -1,25 +0,0 @@ ---- -title: Count Vowels -description: Counts the vowels (a, e, i, o, u) in a string -author: Mcbencrafter -tags: string,vowels,counter ---- - -```java -public static int countVowels(String text) { - String vowels = "aeiou"; - int count = 0; - - for (char character : text.toLowerCase().toCharArray()) { - if (vowels.indexOf(character) == -1) - continue; - - count++; - } - - return count; -} - -// Usage: -System.out.println(countVowels("hello world")); // 3 -``` \ No newline at end of file From c7b4d830ac270d46891375528c8f2859b3598e09 Mon Sep 17 00:00:00 2001 From: Mcbencrafter <92606492+Mcbencrafter@users.noreply.github.com> Date: Sun, 19 Jan 2025 10:34:53 +0100 Subject: [PATCH 20/21] fixed remove punctuation snippet --- snippets/java/string-manipulation/remove-punctuation.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/snippets/java/string-manipulation/remove-punctuation.md b/snippets/java/string-manipulation/remove-punctuation.md index d144e3a4..5074f744 100644 --- a/snippets/java/string-manipulation/remove-punctuation.md +++ b/snippets/java/string-manipulation/remove-punctuation.md @@ -7,7 +7,7 @@ tags: string,punctuation,clean,normalization ```java public static String removePunctuation(String text) { - return text.replaceAll("[,!.]", ""); + return text.replaceAll("[,!.?;:]", ""); } // Usage: From 9f019a9e9c1fded4485c2eb33bf67ac525305878 Mon Sep 17 00:00:00 2001 From: Mcbencrafter <92606492+Mcbencrafter@users.noreply.github.com> Date: Sun, 19 Jan 2025 10:43:40 +0100 Subject: [PATCH 21/21] reset pre-commit file to origin/main because i messed up --- .husky/pre-commit | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/.husky/pre-commit b/.husky/pre-commit index dd8ad688..3b9f6bcf 100644 --- a/.husky/pre-commit +++ b/.husky/pre-commit @@ -1,5 +1,5 @@ -npm run snippets:check -npm run cspell -npm run lint -npm run test-ci -npm run build \ No newline at end of file +npm run snippets:check +npm run cspell +npm run lint +npm run test-ci +npm run build