teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

1

見直しキャンペーン中

2023/07/23 06:09

投稿

TN8001
TN8001

スコア10111

answer CHANGED
@@ -1,65 +1,65 @@
1
- ```C#
2
- textValue = string.Concat(textBox1.Text.Replace(" ", "").Replace(" ", ""));
3
- ```
4
- は、`textBox1`に入っている文字列の空白を削除し`textValue`に代入します(`Concat`はいらないが、あってもエラーではないです)
5
- つまり前の行でやった逆並び変換は、上書きされて**なかったこと**になってしまいます。
6
-
7
-
8
- 目的の処理をわかりやすく各変数を割り当てるとこうなります。
9
- ```C#
10
- private void button1_Click(object sender, RoutedEventArgs e)
11
- {
12
- // textBox1に入力したテキストを取得
13
- string inputText = textBox1.Text;
14
-
15
- //取得した文字列を逆並びに変換
16
- string reverseText = string.Concat(inputText.Reverse());
17
-
18
- //逆並びした文字列の空白を削除
19
- string trimText = reverseText.Replace(" ", "").Replace(" ", "");
20
-
21
- //空白を削除した文字列をtextBox2に出力
22
- textBox2.Text = trimText;
23
- }
24
- ```
25
-
26
- この程度の処理であれば変数名を考えるのも手間なので、同じ変数に再代入することもあります。
27
- ```C#
28
- private void button1_Click(object sender, RoutedEventArgs e)
29
- {
30
- // textBox1に入力したテキストを取得
31
- string text = textBox1.Text;
32
-
33
- //取得した文字列を逆並びに変換
34
- text = string.Concat(text.Reverse());
35
-
36
- //逆並びした文字列の空白を削除
37
- text = text.Replace(" ", "").Replace(" ", "");
38
-
39
- //空白を削除した文字列をtextBox2に出力
40
- textBox2.Text = text;
41
- }
42
- ```
43
-
44
- 自分ではわかりきっているところ(`textBox1`から`textBox2`に入れる)を省いて、このくらいにしてもいいでしょう。
45
- ```C#
46
- private void button1_Click(object sender, RoutedEventArgs e)
47
- {
48
- //逆並び変換
49
- string text = string.Concat(textBox1.Text.Reverse());
50
- //空白を削除
51
- textBox2.Text = text.Replace(" ", "").Replace(" ", "");
52
- }
53
- ```
54
-
55
- 1行で書くこともできますが、やりすぎるとわかりにくくなります。
56
- ```C#
57
- private void button1_Click(object sender, RoutedEventArgs e)
58
- {
59
- textBox2.Text = string.Concat(textBox1.Text.Reverse()).Replace(" ", "").Replace(" ", "");
60
- }
61
- ```
62
-
63
- ---
64
-
1
+ ```cs
2
+ textValue = string.Concat(textBox1.Text.Replace(" ", "").Replace(" ", ""));
3
+ ```
4
+ は、`textBox1`に入っている文字列の空白を削除し`textValue`に代入します(`Concat`はいらないが、あってもエラーではないです)
5
+ つまり前の行でやった逆並び変換は、上書きされて**なかったこと**になってしまいます。
6
+
7
+
8
+ 目的の処理をわかりやすく各変数を割り当てるとこうなります。
9
+ ```cs
10
+ private void button1_Click(object sender, RoutedEventArgs e)
11
+ {
12
+ // textBox1に入力したテキストを取得
13
+ string inputText = textBox1.Text;
14
+
15
+ //取得した文字列を逆並びに変換
16
+ string reverseText = string.Concat(inputText.Reverse());
17
+
18
+ //逆並びした文字列の空白を削除
19
+ string trimText = reverseText.Replace(" ", "").Replace(" ", "");
20
+
21
+ //空白を削除した文字列をtextBox2に出力
22
+ textBox2.Text = trimText;
23
+ }
24
+ ```
25
+
26
+ この程度の処理であれば変数名を考えるのも手間なので、同じ変数に再代入することもあります。
27
+ ```cs
28
+ private void button1_Click(object sender, RoutedEventArgs e)
29
+ {
30
+ // textBox1に入力したテキストを取得
31
+ string text = textBox1.Text;
32
+
33
+ //取得した文字列を逆並びに変換
34
+ text = string.Concat(text.Reverse());
35
+
36
+ //逆並びした文字列の空白を削除
37
+ text = text.Replace(" ", "").Replace(" ", "");
38
+
39
+ //空白を削除した文字列をtextBox2に出力
40
+ textBox2.Text = text;
41
+ }
42
+ ```
43
+
44
+ 自分ではわかりきっているところ(`textBox1`から`textBox2`に入れる)を省いて、このくらいにしてもいいでしょう。
45
+ ```cs
46
+ private void button1_Click(object sender, RoutedEventArgs e)
47
+ {
48
+ //逆並び変換
49
+ string text = string.Concat(textBox1.Text.Reverse());
50
+ //空白を削除
51
+ textBox2.Text = text.Replace(" ", "").Replace(" ", "");
52
+ }
53
+ ```
54
+
55
+ 1行で書くこともできますが、やりすぎるとわかりにくくなります。
56
+ ```cs
57
+ private void button1_Click(object sender, RoutedEventArgs e)
58
+ {
59
+ textBox2.Text = string.Concat(textBox1.Text.Reverse()).Replace(" ", "").Replace(" ", "");
60
+ }
61
+ ```
62
+
63
+ ---
64
+
65
65
  改行(`\r\n`)がある場合は、もうひと手間入れる必要がありますね。