回答編集履歴
1
char 1文字置き換え版 コード(差分)追加
answer
CHANGED
@@ -41,4 +41,16 @@
|
|
41
41
|
System.out.println(str);
|
42
42
|
}
|
43
43
|
}
|
44
|
+
```
|
45
|
+
|
46
|
+
char型配列にして一文字を置換するなら、置き換えの部分を以下のようにする感じで如何でしょう。
|
47
|
+
```java
|
48
|
+
//置き換え
|
49
|
+
char[] strChars = str.toCharArray(); //これで str の全文字が char 型になります
|
50
|
+
char targetChar = target.charAt(0); //置換元文字列の最初の文字
|
51
|
+
char replacementChar = replacement.charAt(0); //置換先文字列の最初の文字
|
52
|
+
for(int i=0; i<strChars.length; i++) {
|
53
|
+
if(strChars[i] == targetChar) strChars[i] = replacementChar;
|
54
|
+
}
|
55
|
+
str = new String(strChars);
|
44
56
|
```
|