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/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/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/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 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-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-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 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 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 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/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/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 diff --git a/snippets/java/string-manipulation/remove-punctuation.md b/snippets/java/string-manipulation/remove-punctuation.md new file mode 100644 index 00000000..5074f744 --- /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 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-word-order.md b/snippets/java/string-manipulation/reverse-word-order.md new file mode 100644 index 00000000..7e18b6de --- /dev/null +++ b/snippets/java/string-manipulation/reverse-word-order.md @@ -0,0 +1,22 @@ +--- +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 +--- + +```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 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 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/spaces-to-tabs.md b/snippets/java/string-manipulation/spaces-to-tabs.md new file mode 100644 index 00000000..91300110 --- /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 convertSpacesToTab(String text, int spacesPerTab) { + return text.replaceAll(" ".repeat(spacesPerTab), "\t"); +} + +// Usage: +System.out.println(convertSpacesToTab("hello world", 4)); // Output: hello\tworld +``` \ 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-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 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/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 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 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