From b7aa6a2caaef6dbbac7740bd67d0a3a7157ad314 Mon Sep 17 00:00:00 2001 From: Swapan Pramanick Date: Sun, 2 Sep 2018 10:38:12 +0530 Subject: [PATCH] BAEL-2140 (#5092) --- .../string/AppendCharAtPositionX.java | 45 +++++++ .../string/AppendCharAtPositionXUnitTest.java | 110 ++++++++++++++++++ 2 files changed, 155 insertions(+) create mode 100644 core-java/src/main/java/com/baeldung/string/AppendCharAtPositionX.java create mode 100644 core-java/src/test/java/com/baeldung/string/AppendCharAtPositionXUnitTest.java diff --git a/core-java/src/main/java/com/baeldung/string/AppendCharAtPositionX.java b/core-java/src/main/java/com/baeldung/string/AppendCharAtPositionX.java new file mode 100644 index 0000000000..bebffe52f1 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/string/AppendCharAtPositionX.java @@ -0,0 +1,45 @@ +/** + * + */ +package com.baeldung.string; + +/** + * @author swpraman + * + */ +public class AppendCharAtPositionX { + + public String addCharUsingCharArray(String str, char ch, int position) { + validate(str, position); + int len = str.length(); + char[] updatedArr = new char[len + 1]; + str.getChars(0, position, updatedArr, 0); + updatedArr[position] = ch; + str.getChars(position, len, updatedArr, position + 1); + return new String(updatedArr); + } + + public String addCharUsingSubstring(String str, char ch, int position) { + validate(str, position); + return str.substring(0, position) + ch + str.substring(position); + } + + public String addCharUsingStringBuilder(String str, char ch, int position) { + validate(str, position); + StringBuilder sb = new StringBuilder(str); + sb.insert(position, ch); + return sb.toString(); + } + + private void validate(String str, int position) { + if (str == null) { + throw new IllegalArgumentException("Str should not be null"); + } + int len = str.length(); + if (position < 0 || position > len) { + throw new IllegalArgumentException("position[" + position + "] should be " + + "in the range 0.." + len + " for string " + str); + } + } + +} diff --git a/core-java/src/test/java/com/baeldung/string/AppendCharAtPositionXUnitTest.java b/core-java/src/test/java/com/baeldung/string/AppendCharAtPositionXUnitTest.java new file mode 100644 index 0000000000..2cdf6145d3 --- /dev/null +++ b/core-java/src/test/java/com/baeldung/string/AppendCharAtPositionXUnitTest.java @@ -0,0 +1,110 @@ +/** + * + */ +package com.baeldung.string; + +import static org.junit.Assert.assertEquals; + +import org.junit.Test; + +/** + * @author swpraman + * + */ +public class AppendCharAtPositionXUnitTest { + + private AppendCharAtPositionX appendCharAtPosition = new AppendCharAtPositionX(); + private String word = "Titanc"; + private char letter = 'i'; + + @Test + public void whenUsingCharacterArrayAndCharacterAddedAtBeginning_shouldAddCharacter() { + assertEquals("iTitanc", appendCharAtPosition.addCharUsingCharArray(word, letter, 0)); + } + + @Test + public void whenUsingSubstringAndCharacterAddedAtBeginning_shouldAddCharacter() { + assertEquals("iTitanc", appendCharAtPosition.addCharUsingSubstring(word, letter, 0)); + } + + @Test + public void whenUsingStringBuilderAndCharacterAddedAtBeginning_shouldAddCharacter() { + assertEquals("iTitanc", appendCharAtPosition.addCharUsingStringBuilder(word, letter, 0)); + } + + @Test + public void whenUsingCharacterArrayAndCharacterAddedAtMiddle_shouldAddCharacter() { + assertEquals("Titianc", appendCharAtPosition.addCharUsingCharArray(word, letter, 3)); + } + + @Test + public void whenUsingSubstringAndCharacterAddedAtMiddle_shouldAddCharacter() { + assertEquals("Titianc", appendCharAtPosition.addCharUsingSubstring(word, letter, 3)); + } + + @Test + public void whenUsingStringBuilderAndCharacterAddedAtMiddle_shouldAddCharacter() { + assertEquals("Titianc", appendCharAtPosition.addCharUsingStringBuilder(word, letter, 3)); + } + + @Test + public void whenUsingCharacterArrayAndCharacterAddedAtEnd_shouldAddCharacter() { + assertEquals("Titanci", appendCharAtPosition.addCharUsingCharArray(word, letter, word.length())); + } + + @Test + public void whenUsingSubstringAndCharacterAddedAtEnd_shouldAddCharacter() { + assertEquals("Titanci", appendCharAtPosition.addCharUsingSubstring(word, letter, word.length())); + } + + @Test + public void whenUsingStringBuilderAndCharacterAddedAtEnd_shouldAddCharacter() { + assertEquals("Titanci", appendCharAtPosition.addCharUsingStringBuilder(word, letter, word.length())); + } + + @Test(expected=IllegalArgumentException.class) + public void whenUsingCharacterArrayAndCharacterAddedAtNegativePosition_shouldThrowException() { + appendCharAtPosition.addCharUsingStringBuilder(word, letter, -1); + } + + @Test(expected=IllegalArgumentException.class) + public void whenUsingSubstringAndCharacterAddedAtNegativePosition_shouldThrowException() { + appendCharAtPosition.addCharUsingStringBuilder(word, letter, -1); + } + + @Test(expected=IllegalArgumentException.class) + public void whenUsingStringBuilderAndCharacterAddedAtNegativePosition_shouldThrowException() { + appendCharAtPosition.addCharUsingStringBuilder(word, letter, -1); + } + + @Test(expected=IllegalArgumentException.class) + public void whenUsingCharacterArrayAndCharacterAddedAtInvalidPosition_shouldThrowException() { + appendCharAtPosition.addCharUsingStringBuilder(word, letter, word.length() + 2); + } + + @Test(expected=IllegalArgumentException.class) + public void whenUsingSubstringAndCharacterAddedAtInvalidPosition_shouldThrowException() { + appendCharAtPosition.addCharUsingStringBuilder(word, letter, word.length() + 2); + } + + @Test(expected=IllegalArgumentException.class) + public void whenUsingStringBuilderAndCharacterAddedAtInvalidPosition_shouldThrowException() { + appendCharAtPosition.addCharUsingStringBuilder(word, letter, word.length() + 2); + } + + @Test(expected=IllegalArgumentException.class) + public void whenUsingCharacterArrayAndCharacterAddedAtPositionXAndStringIsNull_shouldThrowException() { + appendCharAtPosition.addCharUsingStringBuilder(null, letter, 3); + } + + @Test(expected=IllegalArgumentException.class) + public void whenUsingSubstringAndCharacterAddedAtPositionXAndStringIsNull_shouldThrowException() { + appendCharAtPosition.addCharUsingStringBuilder(null, letter, 3); + } + + @Test(expected=IllegalArgumentException.class) + public void whenUsingStringBuilderAndCharacterAddedAtPositionXAndStringIsNull_shouldThrowException() { + appendCharAtPosition.addCharUsingStringBuilder(null, letter, 3); + } + +}