Added Character Array method (#5965)

* Added the code of Replace char in a string at specific index

* added test cases

* Renamed the Test class name end with UnitTest

* Renamed the Test class name end with UnitTest

* Renamed the Test class name end with UnitTest

* Replaced README with the upstream repo

* Added Char Array Method
This commit is contained in:
Shashank agarwal 2018-12-23 20:57:01 +05:30 committed by KevinGilmore
parent be2e45b0a5
commit fda7ff2690
2 changed files with 23 additions and 9 deletions

View File

@ -1,11 +1,19 @@
package com.baeldung.string; package com.baeldung.string;
public class ReplaceCharacterInString { public class ReplaceCharacterInString {
public String replaceCharSubstring(String str, char ch, int index) { public String replaceCharSubstring(String str, char ch, int index) {
String myString = str.substring(0, index) + ch + str.substring(index+1); String myString = str.substring(0, index) + ch + str.substring(index + 1);
return myString; return myString;
} }
public String replaceCharUsingCharArray(String str, char ch, int index) {
char[] chars = str.toCharArray();
chars[index] = ch;
return String.valueOf(chars);
}
public String replaceCharStringBuilder(String str, char ch, int index) { public String replaceCharStringBuilder(String str, char ch, int index) {
StringBuilder myString = new StringBuilder(str); StringBuilder myString = new StringBuilder(str);
myString.setCharAt(index, ch); myString.setCharAt(index, ch);

View File

@ -1,23 +1,29 @@
package com.baeldung.string; package com.baeldung.string;
import org.junit.Test; import org.junit.Test;
import static org.junit.Assert.*; import static org.junit.Assert.*;
public class ReplaceCharInStringUnitTest { public class ReplaceCharInStringUnitTest {
private ReplaceCharacterInString characterInString = new ReplaceCharacterInString(); private ReplaceCharacterInString characterInString = new ReplaceCharacterInString();
@Test @Test
public void whenReplaceCharAtIndexUsingSubstring_thenSuccess(){ public void whenReplaceCharAtIndexUsingSubstring_thenSuccess() {
assertEquals("abcme",characterInString.replaceCharSubstring("abcde",'m',3)); assertEquals("abcme", characterInString.replaceCharSubstring("abcde", 'm', 3));
} }
@Test @Test
public void whenReplaceCharAtIndexUsingStringBuilder_thenSuccess(){ public void whenReplaceCharAtIndexUsingCharArray_thenSuccess() {
assertEquals("abcme",characterInString.replaceCharStringBuilder("abcde",'m',3)); assertEquals("abcme", characterInString.replaceCharUsingCharArray("abcde", 'm', 3));
} }
@Test @Test
public void whenReplaceCharAtIndexUsingStringBuffer_thenSuccess(){ public void whenReplaceCharAtIndexUsingStringBuilder_thenSuccess() {
assertEquals("abcme",characterInString.replaceCharStringBuffer("abcde",'m',3)); assertEquals("abcme", characterInString.replaceCharStringBuilder("abcde", 'm', 3));
}
@Test
public void whenReplaceCharAtIndexUsingStringBuffer_thenSuccess() {
assertEquals("abcme", characterInString.replaceCharStringBuffer("abcde", 'm', 3));
} }
} }