From 5590e0c7baa5fab2ca43fd729b58f61c760981f9 Mon Sep 17 00:00:00 2001 From: Doston Nabotov Date: Sat, 8 Feb 2025 18:14:43 +0200 Subject: [PATCH] Fix line endings --- .gitattributes | 10 ++- .prettierrc | 3 +- VISION.md | 9 ++- public/favicon/android-chrome-192x192.png | Bin 14711 -> 14710 bytes public/favicon/android-chrome-512x512.png | Bin 57862 -> 57860 bytes public/favicon/apple-touch-icon.png | Bin 13099 -> 13098 bytes public/favicon/favicon-16x16.png | Bin 566 -> 565 bytes public/favicon/favicon-32x32.png | Bin 1321 -> 1320 bytes .../SourceSans3-Italic-VariableFont_wght.ttf | Bin 395372 -> 395357 bytes .../fonts/SourceSans3-VariableFont_wght.ttf | Bin 647344 -> 647306 bytes public/preview.png | Bin 169164 -> 169161 bytes snippets/c/bit-manipulation/clear-ith-bit.md | 32 ++++---- snippets/c/bit-manipulation/count-set-bits.md | 44 +++++------ snippets/c/bit-manipulation/get-ith-bit.md | 36 ++++----- snippets/c/bit-manipulation/is-odd.md | 32 ++++---- snippets/c/bit-manipulation/set-ith-bit.md | 36 ++++----- snippets/c/bit-manipulation/swap-numbers.md | 38 ++++----- snippets/c/bit-manipulation/toggle-ith-bit.md | 34 ++++---- .../array-manipulation/remove-duplicates.md | 42 +++++----- .../java/bit-manipulation/bit-counting.md | 44 +++++------ .../java/bit-manipulation/is-power-of-two.md | 32 ++++---- snippets/java/math/checksum.md | 46 +++++------ snippets/java/math/factorial.md | 48 ++++++------ snippets/java/math/fibonacci.md | 36 ++++----- snippets/java/math/greatest-common-divisor.md | 44 +++++------ snippets/java/math/least-common-multiple.md | 50 ++++++------ snippets/java/math/prime-check.md | 60 +++++++------- .../string-manipulation/ascii-to-string.md | 44 +++++------ .../camelcase-to-snake-case.md | 28 +++---- .../string-manipulation/capitalize-words.md | 52 ++++++------ .../java/string-manipulation/check-anagram.md | 54 ++++++------- .../string-manipulation/check-palindrome.md | 38 ++++----- .../count-character-frequency.md | 52 ++++++------ .../count-character-occurrences.md | 50 ++++++------ .../java/string-manipulation/count-words.md | 28 +++---- .../extract-text-between-delimiters.md | 40 +++++----- .../string-manipulation/find-longest-word.md | 48 ++++++------ .../find-unique-characters.md | 48 ++++++------ .../java/string-manipulation/mask-text.md | 48 ++++++------ .../normalize-whitespace.md | 28 +++---- .../string-manipulation/password-generator.md | 74 +++++++++--------- .../string-manipulation/remove-punctuation.md | 28 +++---- .../remove-special-characters.md | 28 +++---- .../reverse-word-contents.md | 44 +++++------ .../string-manipulation/reverse-word-order.md | 42 +++++----- .../string-manipulation/slugify-string.md | 60 +++++++------- .../snake-case-to-camelcase.md | 36 ++++----- .../string-manipulation/spaces-to-tabs.md | 28 +++---- .../string-manipulation/string-to-ascii.md | 46 +++++------ .../string-to-camelcase.md | 48 ++++++------ .../string-to-param-case.md | 28 +++---- .../string-to-pascalcase.md | 44 +++++------ .../string-to-snake-case.md | 28 +++---- .../string-to-titlecase.md | 54 ++++++------- .../string-manipulation/string-to-unicode.md | 40 +++++----- .../string-manipulation/tabs-to-spaces.md | 28 +++---- .../string-manipulation/truncate-string.md | 34 ++++---- .../string-manipulation/unicode-to-string.md | 44 +++++------ 58 files changed, 989 insertions(+), 979 deletions(-) diff --git a/.gitattributes b/.gitattributes index d01653a8..2c0e66db 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1 +1,9 @@ -text eol=lf +* text=auto +*.js text eol=lf +*.jsx text eol=lf +*.ts text eol=lf +*.tsx text eol=lf +*.json text eol=lf +*.css text eol=lf +*.md text eol=lf + diff --git a/.prettierrc b/.prettierrc index f70b340c..ec9891ae 100644 --- a/.prettierrc +++ b/.prettierrc @@ -7,5 +7,6 @@ "bracketSpacing": true, "bracketSameLine": false, "arrowParens": "always", - "jsxSingleQuote": false + "jsxSingleQuote": false, + "endOfLine": "lf" } diff --git a/VISION.md b/VISION.md index 8ab4c6f4..8b92db58 100644 --- a/VISION.md +++ b/VISION.md @@ -28,15 +28,16 @@ We do **NOT** aim to be: ### v1.0 (Launching Soon) - [x] A new snippets storage system for better maintainability and scalability -- [ ] Search functionality -- [ ] A support for frameworks and libraries -- [ ] An ability to share snippets with others -- [ ] A basic SEO (Search Engine Optimization) +- [x] Search functionality +- [x] A support for frameworks and libraries +- [x] An ability to share snippets with others +- [x] A basic SEO (Search Engine Optimization) ### v2.0 (Planned Vision) - [ ] An ability to have private snippets with personal account - [ ] Improved search functionality with filters +- [ ] A documentation page - [ ] An improved SEO (Search Engine Optimization) ### Potential Future Ideas diff --git a/public/favicon/android-chrome-192x192.png b/public/favicon/android-chrome-192x192.png index f7dc6f071d93a5df9c5fa56d30ceca9844a8de75..3347ae075faf86a20da4e56e9d58d8361a18cc58 100644 GIT binary patch delta 13 Ucmexf^sR`cGr-S%BWsZ*04})&r2qf` delta 14 VcmexX^u36+Gr-TCcOy%YB>*!+1+oAD diff --git a/public/favicon/android-chrome-512x512.png b/public/favicon/android-chrome-512x512.png index 1d0abbc10483fbf95a604bd6f92c8778f62da175..025f7838a964db68b2d88a4e604b78eb8667313c 100644 GIT binary patch delta 20 ccmZoW!rXF%nWZzp&wV4S&(zI9Q@8j408_aLJOBUy delta 23 fcmZoU!rXR*nYA;(&z*N8i_cU>-pv71xA*`6WAX@9 diff --git a/public/favicon/apple-touch-icon.png b/public/favicon/apple-touch-icon.png index 82c398351ffb2ecefd8d86987b5c094683d37536..2ddcddee4bbdc0a2ed42688082743fe676bc2684 100644 GIT binary patch delta 13 UcmZ3TwknOKGr-S%BdeM*044JTqW}N^ delta 14 VcmZ3LwmOZqGr-TCcO#3MF#sve1h4=A diff --git a/public/favicon/favicon-16x16.png b/public/favicon/favicon-16x16.png index 28b90c6db1384ebf2f18df638d149ecf7bafb13f..fb0a5a4994497ba3575b72c3981f7ad201951f2b 100644 GIT binary patch delta 13 UcmdnSvXzCUGr-S%BdZ}303LJ$&j0`b delta 14 VcmdnWvWCD78b^DGROdO`u5Are_ZU4f{d_{QsNkwKEp6NhR zemkokvo7;?V?*Wxy4%0RF|$sZ{_6wtx#cH6GP7(y^_5wYX*!T8J^lZ8W)BD}`3LjG W?WRAOmDpP&SXv`kw??puZ~y?KA27cF delta 154 zcmcaRLE_B>iH0qVE1Vg5+kwRP70yh2QyF=;Z@a<7VamumeIGBg(f0Se%vXhhoCmth z^4lLPGXLcUssM6)n73;iG9T34{yL7CZ4ywy#}CZs7N_l+zl|(i+dYHJwMi3;8q|Wrr5s#ZvR9CxIa=GI3o3 z;=)eA8W!6rl{Li{9!)X(icD6wXV5s(N5_%&^lHSGbRU8frnfZqMyS}Y{MmuNRVuZi zvP)kvoE%d(;pL2khT1vh1;(z(6u7>j3xR2y4h86rDiY5RBuQmHDFLrc8sqng24e6- zD2TxZ6}mVmwT&htq!`y@%g3z!V$p>SKZD)Wjl}RXES4TFbMGp#q;K V)%QNvHXX&jW}o!6z)^vp+&>m&Z;=21 delta 323 zcmeA=sJ`K#dczjRt)Cfrw*!f49!B1&2267pdAFWn(o+Bm{!(DtrU;}rI5BlH7(qsvw zIcizfi2&(s6Iq^!1L;{SS(I6T^xm~BOpHK!|2mdprsLCj^*LIcHETuARyj<*z%?#5M z=dp-x-^|WhAqva<#Z$>1mx&+Wa`wLlffs{@W>k%OJp@=mHNW~Ph n9t5h9En!`)1mvEW#(I&Fw|(YxRv=~rVs;?r*gkVQ=SL3!R$y$} diff --git a/public/preview.png b/public/preview.png index 2151f06e55e34f594ddf35d384d047140c2c4ec0..be216da147fb145d8f625155bbfefed6b0a0e854 100644 GIT binary patch delta 36 ucmV+<0Nek}sS3%d3IvHzPDg> i) & 1; -} - - -// Usage: -get_ith_bit(10, 0); // Returns: 0 -get_ith_bit(10, 1); // Returns: 1 -get_ith_bit(10, 2); // Returns: 0 -get_ith_bit(10, 3); // Returns: 1 +--- +title: Get ith bit +description: Get the i-th bit of a number +tags: bit-manipulation, number, get +author: aelshinawy +--- + +```c +int get_ith_bit(int n, int i) { + return (n >> i) & 1; +} + + +// Usage: +get_ith_bit(10, 0); // Returns: 0 +get_ith_bit(10, 1); // Returns: 1 +get_ith_bit(10, 2); // Returns: 0 +get_ith_bit(10, 3); // Returns: 1 ``` \ No newline at end of file diff --git a/snippets/c/bit-manipulation/is-odd.md b/snippets/c/bit-manipulation/is-odd.md index 754f7684..24a86f6b 100644 --- a/snippets/c/bit-manipulation/is-odd.md +++ b/snippets/c/bit-manipulation/is-odd.md @@ -1,17 +1,17 @@ ---- -title: Is Odd -description: Check if a number is odd -tags: bit-manipulation, number, is-odd -author: aelshinawy ---- - -```c -bool is_odd(int n) { - return n & 1; -} - - -// Usage: -is_odd(10); // Returns: false -is_odd(11); // Returns: true +--- +title: Is Odd +description: Check if a number is odd +tags: bit-manipulation, number, is-odd +author: aelshinawy +--- + +```c +bool is_odd(int n) { + return n & 1; +} + + +// Usage: +is_odd(10); // Returns: false +is_odd(11); // Returns: true ``` \ No newline at end of file diff --git a/snippets/c/bit-manipulation/set-ith-bit.md b/snippets/c/bit-manipulation/set-ith-bit.md index de1d86c3..84ed52c8 100644 --- a/snippets/c/bit-manipulation/set-ith-bit.md +++ b/snippets/c/bit-manipulation/set-ith-bit.md @@ -1,19 +1,19 @@ ---- -title: Set ith bit -description: Set the i-th bit of a number and returns the resulting number -tags: bit-manipulation, number, set -author: aelshinawy ---- - -```c -int set_ith_bit(int n, int i) { - return n | (1 << i); -} - - -// Usage: -set_ith_bit(10, 0); // Returns: 11 -set_ith_bit(10, 2); // Returns: 14 -set_ith_bit(1, 8); // Returns: 257 -set_ith_bit(1, 3); // Returns: 9 +--- +title: Set ith bit +description: Set the i-th bit of a number and returns the resulting number +tags: bit-manipulation, number, set +author: aelshinawy +--- + +```c +int set_ith_bit(int n, int i) { + return n | (1 << i); +} + + +// Usage: +set_ith_bit(10, 0); // Returns: 11 +set_ith_bit(10, 2); // Returns: 14 +set_ith_bit(1, 8); // Returns: 257 +set_ith_bit(1, 3); // Returns: 9 ``` \ No newline at end of file diff --git a/snippets/c/bit-manipulation/swap-numbers.md b/snippets/c/bit-manipulation/swap-numbers.md index 14a377f1..79669ad0 100644 --- a/snippets/c/bit-manipulation/swap-numbers.md +++ b/snippets/c/bit-manipulation/swap-numbers.md @@ -1,20 +1,20 @@ ---- -title: Swap Numbers -description: Swap two numbers without a temporary variable -tags: bit-manipulation, number, swap -author: aelshinawy ---- - -```c -void swap(int *a, int *b) { - *a ^= *b; - *b ^= *a; - *a ^= *b; -} - - -// Usage: -int x = 5, y = 10; -swap(&x, &y); -printf("x = %d, y = %d\n", x, y); // x = 10, y = 5 +--- +title: Swap Numbers +description: Swap two numbers without a temporary variable +tags: bit-manipulation, number, swap +author: aelshinawy +--- + +```c +void swap(int *a, int *b) { + *a ^= *b; + *b ^= *a; + *a ^= *b; +} + + +// Usage: +int x = 5, y = 10; +swap(&x, &y); +printf("x = %d, y = %d\n", x, y); // x = 10, y = 5 ``` \ No newline at end of file diff --git a/snippets/c/bit-manipulation/toggle-ith-bit.md b/snippets/c/bit-manipulation/toggle-ith-bit.md index 0c3fd0fa..d8d7cc1b 100644 --- a/snippets/c/bit-manipulation/toggle-ith-bit.md +++ b/snippets/c/bit-manipulation/toggle-ith-bit.md @@ -1,18 +1,18 @@ ---- -title: Toggle ith bit -description: Toggle the i-th bit of a number and returns the resulting number -tags: bit-manipulation, number, toggle -author: aelshinawy ---- - -```c -int toggle_ith_bit(int n, int i) { - return n ^ (1 << i); -} - - -// Usage: -toggle_ith_bit(10, 0); // Returns: 11 -toggle_ith_bit(10, 1); // Returns: 8 -toggle_ith_bit(8, 1); // Returns: 10 +--- +title: Toggle ith bit +description: Toggle the i-th bit of a number and returns the resulting number +tags: bit-manipulation, number, toggle +author: aelshinawy +--- + +```c +int toggle_ith_bit(int n, int i) { + return n ^ (1 << i); +} + + +// Usage: +toggle_ith_bit(10, 0); // Returns: 11 +toggle_ith_bit(10, 1); // Returns: 8 +toggle_ith_bit(8, 1); // Returns: 10 ``` \ No newline at end of file diff --git a/snippets/java/array-manipulation/remove-duplicates.md b/snippets/java/array-manipulation/remove-duplicates.md index 4f25da30..a6fc9ef8 100644 --- a/snippets/java/array-manipulation/remove-duplicates.md +++ b/snippets/java/array-manipulation/remove-duplicates.md @@ -1,22 +1,22 @@ ---- -title: Remove duplicates -description: Removes duplicate elements from an list -author: Mcbencrafter -tags: list,duplicates,unique ---- - -```java -import java.util.List; -import java.util.stream.Collectors; - -public static List removeDuplicates(List list) { - return list.stream() - .distinct() - .collect(Collectors.toList()); -} - -// Usage: -List list = List.of(1, 2, 3, 4, 5, 1, 2, 3, 4, 5); -List result = removeDuplicates(list); -System.out.println("List with duplicates removed: " + result); // [1, 2, 3, 4, 5] +--- +title: Remove duplicates +description: Removes duplicate elements from an list +author: Mcbencrafter +tags: list,duplicates,unique +--- + +```java +import java.util.List; +import java.util.stream.Collectors; + +public static List removeDuplicates(List list) { + return list.stream() + .distinct() + .collect(Collectors.toList()); +} + +// Usage: +List list = List.of(1, 2, 3, 4, 5, 1, 2, 3, 4, 5); +List result = removeDuplicates(list); +System.out.println("List with duplicates removed: " + result); // [1, 2, 3, 4, 5] ``` \ No newline at end of file diff --git a/snippets/java/bit-manipulation/bit-counting.md b/snippets/java/bit-manipulation/bit-counting.md index 534e5f7b..8eeb2d2a 100644 --- a/snippets/java/bit-manipulation/bit-counting.md +++ b/snippets/java/bit-manipulation/bit-counting.md @@ -1,23 +1,23 @@ ---- -title: Bit Counting -description: Counts the set bits in the binary representation of an integer -author: Mcbencrafter -tags: math,number,bits,bit-counting ---- - -```java -public static int countBits(int number) { - int bits = 0; - - while (number > 0) { - bits += number & 1; - number >>= 1; - } - - return bits; -} - -// Usage: -int number = 5; -System.out.println(countBits(5)); // 2 (101) +--- +title: Bit Counting +description: Counts the set bits in the binary representation of an integer +author: Mcbencrafter +tags: math,number,bits,bit-counting +--- + +```java +public static int countBits(int number) { + int bits = 0; + + while (number > 0) { + bits += number & 1; + number >>= 1; + } + + return bits; +} + +// Usage: +int number = 5; +System.out.println(countBits(5)); // 2 (101) ``` \ No newline at end of file diff --git a/snippets/java/bit-manipulation/is-power-of-two.md b/snippets/java/bit-manipulation/is-power-of-two.md index 42055f43..d71d26c4 100644 --- a/snippets/java/bit-manipulation/is-power-of-two.md +++ b/snippets/java/bit-manipulation/is-power-of-two.md @@ -1,16 +1,16 @@ ---- -title: Is Power Of Two -description: Checks if a number is a power of two -author: Mcbencrafter -tags: math,number,bit,power-of-two ---- - -```java -public static boolean isPowerOfTwo(int number) { - return (number > 0) && ((number & (number - 1)) == 0); -} - -// Usage: -int number = 16; -System.out.println(isPowerOfTwo(number)); // true (2^4) -``` +--- +title: Is Power Of Two +description: Checks if a number is a power of two +author: Mcbencrafter +tags: math,number,bit,power-of-two +--- + +```java +public static boolean isPowerOfTwo(int number) { + return (number > 0) && ((number & (number - 1)) == 0); +} + +// Usage: +int number = 16; +System.out.println(isPowerOfTwo(number)); // true (2^4) +``` diff --git a/snippets/java/math/checksum.md b/snippets/java/math/checksum.md index d48184a1..adda949b 100644 --- a/snippets/java/math/checksum.md +++ b/snippets/java/math/checksum.md @@ -1,24 +1,24 @@ ---- -title: Checksum -description: Calculates the checksum of an int -author: Mcbencrafter -tags: math,number,checksum ---- - -```java -public static int checksum(int number) { - number = Math.abs(number); - int sum = 0; - - while (number != 0) { - sum += number % 10; - number /= 10; - } - - return sum; -} - -// Usage: -int number = 12345; -System.out.println(checksum(number)); // 15 = 1+2+3+4+5 +--- +title: Checksum +description: Calculates the checksum of an int +author: Mcbencrafter +tags: math,number,checksum +--- + +```java +public static int checksum(int number) { + number = Math.abs(number); + int sum = 0; + + while (number != 0) { + sum += number % 10; + number /= 10; + } + + return sum; +} + +// Usage: +int number = 12345; +System.out.println(checksum(number)); // 15 = 1+2+3+4+5 ``` \ No newline at end of file diff --git a/snippets/java/math/factorial.md b/snippets/java/math/factorial.md index 1b3ff2ed..a9f0ffab 100644 --- a/snippets/java/math/factorial.md +++ b/snippets/java/math/factorial.md @@ -1,24 +1,24 @@ ---- -title: Factorial -description: Computes the factorial of a given number -author: Mcbencrafter -tags: math,number,factorial ---- - -```java -import java.math.BigInteger; - -public static BigInteger factorial(int number) { - BigInteger result = BigInteger.ONE; - - for (int currentNumber = 1; currentNumber <= number; currentNumber++) { - result = result.multiply(BigInteger.valueOf(currentNumber)); - } - - return result; -} - -// Usage: -int number = 6; -System.out.println(factorial(number)); // 720 = 6*5*4*3*2 -``` +--- +title: Factorial +description: Computes the factorial of a given number +author: Mcbencrafter +tags: math,number,factorial +--- + +```java +import java.math.BigInteger; + +public static BigInteger factorial(int number) { + BigInteger result = BigInteger.ONE; + + for (int currentNumber = 1; currentNumber <= number; currentNumber++) { + result = result.multiply(BigInteger.valueOf(currentNumber)); + } + + return result; +} + +// Usage: +int number = 6; +System.out.println(factorial(number)); // 720 = 6*5*4*3*2 +``` diff --git a/snippets/java/math/fibonacci.md b/snippets/java/math/fibonacci.md index c2fb507c..0f07aa96 100644 --- a/snippets/java/math/fibonacci.md +++ b/snippets/java/math/fibonacci.md @@ -1,19 +1,19 @@ ---- -title: Fibonacci -description: Calculates the nth fibonacci number -author: Mcbencrafter -tags: math,number,fibonacci ---- - -```java -public static int fibonacci(int number) { - if (number <= 1) - return number; - - return fibonacci(number - 1) + fibonacci(number - 2); -} - -// Usage: -int number = 5; -System.out.println(fibonacci(number)) // 3 (0, 1, 1, 2, 3) +--- +title: Fibonacci +description: Calculates the nth fibonacci number +author: Mcbencrafter +tags: math,number,fibonacci +--- + +```java +public static int fibonacci(int number) { + if (number <= 1) + return number; + + return fibonacci(number - 1) + fibonacci(number - 2); +} + +// Usage: +int number = 5; +System.out.println(fibonacci(number)) // 3 (0, 1, 1, 2, 3) ``` \ No newline at end of file diff --git a/snippets/java/math/greatest-common-divisor.md b/snippets/java/math/greatest-common-divisor.md index dc3acdb7..8d777ee3 100644 --- a/snippets/java/math/greatest-common-divisor.md +++ b/snippets/java/math/greatest-common-divisor.md @@ -1,23 +1,23 @@ ---- -title: Greatest Common Divisor -description: Calculates the greatest common divisor (gcd) of two numbers -author: Mcbencrafter -tags: math,number,greatest-common-devisor,gcd,euclidean-algorithm ---- - -```java -public static int gcd(int number1, int number2) { - while (number2 != 0) { - int remainder = number2; - number2 = number1 % number2; - number1 = remainder; - } - - return number1; -} - -// Usage: -int a = 16; -int b = 12; -System.out.println(gcd(a, b)); // 4 +--- +title: Greatest Common Divisor +description: Calculates the greatest common divisor (gcd) of two numbers +author: Mcbencrafter +tags: math,number,greatest-common-devisor,gcd,euclidean-algorithm +--- + +```java +public static int gcd(int number1, int number2) { + while (number2 != 0) { + int remainder = number2; + number2 = number1 % number2; + number1 = remainder; + } + + return number1; +} + +// Usage: +int a = 16; +int b = 12; +System.out.println(gcd(a, b)); // 4 ``` \ No newline at end of file diff --git a/snippets/java/math/least-common-multiple.md b/snippets/java/math/least-common-multiple.md index 4180be1e..0ee1de99 100644 --- a/snippets/java/math/least-common-multiple.md +++ b/snippets/java/math/least-common-multiple.md @@ -1,26 +1,26 @@ ---- -title: Least Common Multiple -description: Calculates the least common multiple (lcm) of two numbers -author: Mcbencrafter -tags: math,number,least-common-multiple,lcm,euclidean-algorithm ---- - -```java -public static int lcm(int number1, int number2) { - int gcdNumber1 = number1; - int gcdNumber2 = number2; - - while (gcdNumber2 != 0) { - int remainder = gcdNumber2; - gcdNumber2 = gcdNumber1 % gcdNumber2; - gcdNumber1 = remainder; - } - - return (number1 / gcdNumber1) * number2; -} - -// Usage: -int a = 16; -int b = 12; -System.out.println(lcm(a, b)); // 48 +--- +title: Least Common Multiple +description: Calculates the least common multiple (lcm) of two numbers +author: Mcbencrafter +tags: math,number,least-common-multiple,lcm,euclidean-algorithm +--- + +```java +public static int lcm(int number1, int number2) { + int gcdNumber1 = number1; + int gcdNumber2 = number2; + + while (gcdNumber2 != 0) { + int remainder = gcdNumber2; + gcdNumber2 = gcdNumber1 % gcdNumber2; + gcdNumber1 = remainder; + } + + return (number1 / gcdNumber1) * number2; +} + +// Usage: +int a = 16; +int b = 12; +System.out.println(lcm(a, b)); // 48 ``` \ No newline at end of file diff --git a/snippets/java/math/prime-check.md b/snippets/java/math/prime-check.md index 47b9440a..121ee2a5 100644 --- a/snippets/java/math/prime-check.md +++ b/snippets/java/math/prime-check.md @@ -1,31 +1,31 @@ ---- -title: Prime Check -description: Checks if a number is a prime -author: Mcbencrafter -tags: math,number,prime ---- - -```java -public static boolean isPrime(int number) { - if (number <= 1) - return false; - - if (number <= 3) - return true; - - boolean prime = true; - for (int divisor = 3; divisor < number; divisor++) { - if (number % divisor != 0) - continue; - - prime = false; - break; - } - - return prime; -} - -// Usage: -int number = 31; -System.out.println(isPrime(number)); // true +--- +title: Prime Check +description: Checks if a number is a prime +author: Mcbencrafter +tags: math,number,prime +--- + +```java +public static boolean isPrime(int number) { + if (number <= 1) + return false; + + if (number <= 3) + return true; + + boolean prime = true; + for (int divisor = 3; divisor < number; divisor++) { + if (number % divisor != 0) + continue; + + prime = false; + break; + } + + return prime; +} + +// Usage: +int number = 31; +System.out.println(isPrime(number)); // true ``` \ No newline at end of file diff --git a/snippets/java/string-manipulation/ascii-to-string.md b/snippets/java/string-manipulation/ascii-to-string.md index 8e7cb924..eb544572 100644 --- a/snippets/java/string-manipulation/ascii-to-string.md +++ b/snippets/java/string-manipulation/ascii-to-string.md @@ -1,23 +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" +--- +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 index 86219c7b..1814ab42 100644 --- a/snippets/java/string-manipulation/camelcase-to-snake-case.md +++ b/snippets/java/string-manipulation/camelcase-to-snake-case.md @@ -1,15 +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" +--- +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 index 81327166..360b2f00 100644 --- a/snippets/java/string-manipulation/capitalize-words.md +++ b/snippets/java/string-manipulation/capitalize-words.md @@ -1,27 +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" +--- +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 index f1d27dee..2c0cdfb0 100644 --- a/snippets/java/string-manipulation/check-anagram.md +++ b/snippets/java/string-manipulation/check-anagram.md @@ -1,28 +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 +--- +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 index dde8ac34..9d0da856 100644 --- a/snippets/java/string-manipulation/check-palindrome.md +++ b/snippets/java/string-manipulation/check-palindrome.md @@ -1,20 +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 +--- +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 index 85082d64..f1a8e14a 100644 --- a/snippets/java/string-manipulation/count-character-frequency.md +++ b/snippets/java/string-manipulation/count-character-frequency.md @@ -1,27 +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} +--- +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 index 6906fe14..5b15f8f6 100644 --- a/snippets/java/string-manipulation/count-character-occurrences.md +++ b/snippets/java/string-manipulation/count-character-occurrences.md @@ -1,26 +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 +--- +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 index b0293ef1..7284ac83 100644 --- a/snippets/java/string-manipulation/count-words.md +++ b/snippets/java/string-manipulation/count-words.md @@ -1,15 +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 +--- +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 index cededfb6..e377cde8 100644 --- a/snippets/java/string-manipulation/extract-text-between-delimiters.md +++ b/snippets/java/string-manipulation/extract-text-between-delimiters.md @@ -1,21 +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" +--- +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 index 4279c960..24a62016 100644 --- a/snippets/java/string-manipulation/find-longest-word.md +++ b/snippets/java/string-manipulation/find-longest-word.md @@ -1,25 +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" +--- +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 index b52d91e6..aed5942d 100644 --- a/snippets/java/string-manipulation/find-unique-characters.md +++ b/snippets/java/string-manipulation/find-unique-characters.md @@ -1,25 +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] +--- +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 index c7f51314..cc66ff71 100644 --- a/snippets/java/string-manipulation/mask-text.md +++ b/snippets/java/string-manipulation/mask-text.md @@ -1,25 +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" +--- +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 index 15c0df7a..e9e7f310 100644 --- a/snippets/java/string-manipulation/normalize-whitespace.md +++ b/snippets/java/string-manipulation/normalize-whitespace.md @@ -1,15 +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" +--- +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 index 5e0add08..7a1cffa6 100644 --- a/snippets/java/string-manipulation/password-generator.md +++ b/snippets/java/string-manipulation/password-generator.md @@ -1,38 +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 +--- +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 index 5074f744..9cabdc79 100644 --- a/snippets/java/string-manipulation/remove-punctuation.md +++ b/snippets/java/string-manipulation/remove-punctuation.md @@ -1,15 +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" +--- +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 index a784478b..22c00a84 100644 --- a/snippets/java/string-manipulation/remove-special-characters.md +++ b/snippets/java/string-manipulation/remove-special-characters.md @@ -1,15 +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" +--- +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 index 8112f768..90b1dd6b 100644 --- a/snippets/java/string-manipulation/reverse-word-contents.md +++ b/snippets/java/string-manipulation/reverse-word-contents.md @@ -1,23 +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" +--- +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 index 7e18b6de..1d582c9f 100644 --- a/snippets/java/string-manipulation/reverse-word-order.md +++ b/snippets/java/string-manipulation/reverse-word-order.md @@ -1,22 +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 +--- +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 index b12b2432..46f4c83f 100644 --- a/snippets/java/string-manipulation/slugify-string.md +++ b/snippets/java/string-manipulation/slugify-string.md @@ -1,31 +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" +--- +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 index fd8e3206..58d6829d 100644 --- a/snippets/java/string-manipulation/snake-case-to-camelcase.md +++ b/snippets/java/string-manipulation/snake-case-to-camelcase.md @@ -1,19 +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" +--- +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 index 91300110..1142f263 100644 --- a/snippets/java/string-manipulation/spaces-to-tabs.md +++ b/snippets/java/string-manipulation/spaces-to-tabs.md @@ -1,15 +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 +--- +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 index d5e51458..56fc81aa 100644 --- a/snippets/java/string-manipulation/string-to-ascii.md +++ b/snippets/java/string-manipulation/string-to-ascii.md @@ -1,24 +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] +--- +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 index 5a94713c..daf654d8 100644 --- a/snippets/java/string-manipulation/string-to-camelcase.md +++ b/snippets/java/string-manipulation/string-to-camelcase.md @@ -1,25 +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" +--- +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 index 576d3987..181308db 100644 --- a/snippets/java/string-manipulation/string-to-param-case.md +++ b/snippets/java/string-manipulation/string-to-param-case.md @@ -1,15 +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" +--- +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 index f760f369..120d88da 100644 --- a/snippets/java/string-manipulation/string-to-pascalcase.md +++ b/snippets/java/string-manipulation/string-to-pascalcase.md @@ -1,23 +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" +--- +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 index 69a24762..83c4a36f 100644 --- a/snippets/java/string-manipulation/string-to-snake-case.md +++ b/snippets/java/string-manipulation/string-to-snake-case.md @@ -1,15 +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" +--- +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 index 400fd86a..89d5c9c3 100644 --- a/snippets/java/string-manipulation/string-to-titlecase.md +++ b/snippets/java/string-manipulation/string-to-titlecase.md @@ -1,28 +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" +--- +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 index cd6bc518..7c66775e 100644 --- a/snippets/java/string-manipulation/string-to-unicode.md +++ b/snippets/java/string-manipulation/string-to-unicode.md @@ -1,21 +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 +--- +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 index 23b7f18d..e37163db 100644 --- a/snippets/java/string-manipulation/tabs-to-spaces.md +++ b/snippets/java/string-manipulation/tabs-to-spaces.md @@ -1,15 +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" +--- +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 index 6401e867..38f85281 100644 --- a/snippets/java/string-manipulation/truncate-string.md +++ b/snippets/java/string-manipulation/truncate-string.md @@ -1,18 +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..." +--- +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 index 4846c823..8f0385c7 100644 --- a/snippets/java/string-manipulation/unicode-to-string.md +++ b/snippets/java/string-manipulation/unicode-to-string.md @@ -1,23 +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" +--- +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