From 148627f1d0ea00bcab90e4ebc454ec5aaa494ccd Mon Sep 17 00:00:00 2001 From: Azhwani <13301425+azhwani@users.noreply.github.com> Date: Mon, 3 Jan 2022 05:13:38 +0100 Subject: [PATCH] init commit (#11623) --- .../split/SplitStringEveryNthChar.java | 51 +++++++++++++++++++ .../SplitStringEveryNthCharUnitTest.java | 42 +++++++++++++++ 2 files changed, 93 insertions(+) create mode 100644 core-java-modules/core-java-string-operations-4/src/main/java/com/baeldung/split/SplitStringEveryNthChar.java create mode 100644 core-java-modules/core-java-string-operations-4/src/test/java/com/baeldung/split/SplitStringEveryNthCharUnitTest.java diff --git a/core-java-modules/core-java-string-operations-4/src/main/java/com/baeldung/split/SplitStringEveryNthChar.java b/core-java-modules/core-java-string-operations-4/src/main/java/com/baeldung/split/SplitStringEveryNthChar.java new file mode 100644 index 0000000000..3ac31d012a --- /dev/null +++ b/core-java-modules/core-java-string-operations-4/src/main/java/com/baeldung/split/SplitStringEveryNthChar.java @@ -0,0 +1,51 @@ +package com.baeldung.split; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import com.google.common.base.Splitter; +import com.google.common.collect.ImmutableList; + +public class SplitStringEveryNthChar { + + public static List usingSplitMethod(String text, int n) { + String[] results = text.split("(?<=\\G.{" + n + "})"); + + return Arrays.asList(results); + } + + public static List usingSubstringMethod(String text, int n) { + List results = new ArrayList<>(); + int length = text.length(); + + for (int i = 0; i < length; i += n) { + results.add(text.substring(i, Math.min(length, i + n))); + } + + return results; + } + + public static List usingPattern(String text, int n) { + List results = new ArrayList<>(); + + Pattern pattern = Pattern.compile(".{1," + n + "}"); + Matcher matcher = pattern.matcher(text); + while (matcher.find()) { + String match = text.substring(matcher.start(), matcher.end()); + results.add(match); + } + + return results; + } + + public static List usingGuava(String text, int n) { + Iterable parts = Splitter.fixedLength(n) + .split(text); + + return ImmutableList.copyOf(parts); + } + +} diff --git a/core-java-modules/core-java-string-operations-4/src/test/java/com/baeldung/split/SplitStringEveryNthCharUnitTest.java b/core-java-modules/core-java-string-operations-4/src/test/java/com/baeldung/split/SplitStringEveryNthCharUnitTest.java new file mode 100644 index 0000000000..03dcd5312e --- /dev/null +++ b/core-java-modules/core-java-string-operations-4/src/test/java/com/baeldung/split/SplitStringEveryNthCharUnitTest.java @@ -0,0 +1,42 @@ +package com.baeldung.split; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.collection.IsIterableContainingInOrder.contains; + +import java.util.List; + +import org.junit.jupiter.api.Test; + +public class SplitStringEveryNthCharUnitTest { + + public static final String TEXT = "abcdefgh123456"; + + @Test + public void givenString_whenUsingSplit_thenSplit() { + List results = SplitStringEveryNthChar.usingSplitMethod(TEXT, 3); + + assertThat(results, contains("abc", "def", "gh1", "234", "56")); + } + + @Test + public void givenString_whenUsingSubstring_thenSplit() { + List results = SplitStringEveryNthChar.usingSubstringMethod(TEXT, 4); + + assertThat(results, contains("abcd", "efgh", "1234", "56")); + } + + @Test + public void givenString_whenUsingPattern_thenSplit() { + List results = SplitStringEveryNthChar.usingPattern(TEXT, 5); + + assertThat(results, contains("abcde", "fgh12", "3456")); + } + + @Test + public void givenString_whenUsingGuava_thenSplit() { + List results = SplitStringEveryNthChar.usingGuava(TEXT, 6); + + assertThat(results, contains("abcdef", "gh1234", "56")); + } + +}