Skip to content

Commit fda7ff2

Browse files
shank318KevinGilmore
authored andcommitted
Added Character Array method (eugenp#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
1 parent be2e45b commit fda7ff2

2 files changed

Lines changed: 23 additions & 9 deletions

File tree

java-strings/src/main/java/com/baeldung/string/ReplaceCharacterInString.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,20 @@
11
package com.baeldung.string;
22

33
public class ReplaceCharacterInString {
4-
public String replaceCharSubstring(String str, char ch, int index) {
5-
String myString = str.substring(0, index) + ch + str.substring(index+1);
4+
5+
public String replaceCharSubstring(String str, char ch, int index) {
6+
String myString = str.substring(0, index) + ch + str.substring(index + 1);
67
return myString;
78
}
89

9-
public String replaceCharStringBuilder(String str, char ch, int index) {
10+
public String replaceCharUsingCharArray(String str, char ch, int index) {
11+
char[] chars = str.toCharArray();
12+
chars[index] = ch;
13+
return String.valueOf(chars);
14+
}
15+
16+
17+
public String replaceCharStringBuilder(String str, char ch, int index) {
1018
StringBuilder myString = new StringBuilder(str);
1119
myString.setCharAt(index, ch);
1220
return myString.toString();
Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,29 @@
11
package com.baeldung.string;
22

33
import org.junit.Test;
4+
45
import static org.junit.Assert.*;
56

67
public class ReplaceCharInStringUnitTest {
78
private ReplaceCharacterInString characterInString = new ReplaceCharacterInString();
89

910
@Test
10-
public void whenReplaceCharAtIndexUsingSubstring_thenSuccess(){
11-
assertEquals("abcme",characterInString.replaceCharSubstring("abcde",'m',3));
11+
public void whenReplaceCharAtIndexUsingSubstring_thenSuccess() {
12+
assertEquals("abcme", characterInString.replaceCharSubstring("abcde", 'm', 3));
13+
}
14+
15+
@Test
16+
public void whenReplaceCharAtIndexUsingCharArray_thenSuccess() {
17+
assertEquals("abcme", characterInString.replaceCharUsingCharArray("abcde", 'm', 3));
1218
}
1319

1420
@Test
15-
public void whenReplaceCharAtIndexUsingStringBuilder_thenSuccess(){
16-
assertEquals("abcme",characterInString.replaceCharStringBuilder("abcde",'m',3));
21+
public void whenReplaceCharAtIndexUsingStringBuilder_thenSuccess() {
22+
assertEquals("abcme", characterInString.replaceCharStringBuilder("abcde", 'm', 3));
1723
}
1824

1925
@Test
20-
public void whenReplaceCharAtIndexUsingStringBuffer_thenSuccess(){
21-
assertEquals("abcme",characterInString.replaceCharStringBuffer("abcde",'m',3));
26+
public void whenReplaceCharAtIndexUsingStringBuffer_thenSuccess() {
27+
assertEquals("abcme", characterInString.replaceCharStringBuffer("abcde", 'm', 3));
2228
}
2329
}

0 commit comments

Comments
 (0)