回答編集履歴

1

見直しキャンペーン中

2023/07/23 06:09

投稿

TN8001
TN8001

スコア9326

test CHANGED
@@ -1,129 +1,65 @@
1
- ```C#
1
+ ```cs
2
-
3
2
  textValue = string.Concat(textBox1.Text.Replace(" ", "").Replace(" ", ""));
4
-
5
3
  ```
6
-
7
4
  は、`textBox1`に入っている文字列の空白を削除し`textValue`に代入します(`Concat`はいらないが、あってもエラーではないです)
8
-
9
5
  つまり前の行でやった逆並び変換は、上書きされて**なかったこと**になってしまいます。
10
6
 
11
7
 
12
-
13
-
14
-
15
8
  目的の処理をわかりやすく各変数を割り当てるとこうなります。
16
-
17
- ```C#
9
+ ```cs
18
-
19
10
  private void button1_Click(object sender, RoutedEventArgs e)
20
-
21
11
  {
22
-
23
12
  // textBox1に入力したテキストを取得
24
-
25
13
  string inputText = textBox1.Text;
26
14
 
15
+ //取得した文字列を逆並びに変換
16
+ string reverseText = string.Concat(inputText.Reverse());
27
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;
28
32
 
29
33
  //取得した文字列を逆並びに変換
30
-
31
- string reverseText = string.Concat(inputText.Reverse());
34
+ text = string.Concat(text.Reverse());
32
-
33
-
34
35
 
35
36
  //逆並びした文字列の空白を削除
36
-
37
- string trimText = reverseText.Replace(" ", "").Replace(" ", "");
37
+ text = text.Replace(" ", "").Replace(" ", "");
38
-
39
-
40
38
 
41
39
  //空白を削除した文字列をtextBox2に出力
42
-
43
- textBox2.Text = trimText;
40
+ textBox2.Text = text;
44
-
45
41
  }
46
-
47
42
  ```
48
43
 
49
-
50
-
51
- この程度の処理あれば変数名を考えのも手間なので、同じ変数再代るこあります
44
+ 自分はわかりきっていところ(`textBox1`から`textBox2`に入)を省いて、のくらいにしていいでしょう
52
-
53
- ```C#
45
+ ```cs
54
-
55
46
  private void button1_Click(object sender, RoutedEventArgs e)
56
-
57
47
  {
58
-
59
- // textBox1に入力したテキストを取得
60
-
61
- string text = textBox1.Text;
62
-
63
-
64
-
65
- //取得した文字列を逆並び変換
48
+ //逆並び変換
66
-
67
- text = string.Concat(text.Reverse());
49
+ string text = string.Concat(textBox1.Text.Reverse());
68
-
69
-
70
-
71
- //逆並びした文字列の空白を削除
50
+ //空白を削除
72
-
73
- text = text.Replace(" ", "").Replace(" ", "");
51
+ textBox2.Text = text.Replace(" ", "").Replace(" ", "");
74
-
75
-
76
-
77
- //空白を削除した文字列をtextBox2に出力
78
-
79
- textBox2.Text = text;
80
-
81
52
  }
82
-
83
53
  ```
84
54
 
85
-
86
-
87
- 自分はわかきっているところ(`textBox1`ら`textBox2`入れる)を省いて、このらいにしてもいいでしょう
55
+ 1行書くこともできますが、やすぎるとにくくなります
88
-
89
- ```C#
56
+ ```cs
90
-
91
57
  private void button1_Click(object sender, RoutedEventArgs e)
92
-
93
58
  {
94
-
95
- //逆並び変換
96
-
97
- string text = string.Concat(textBox1.Text.Reverse());
98
-
99
- //空白を削除
100
-
101
- textBox2.Text = text.Replace(" ", "").Replace(" ", "");
59
+ textBox2.Text = string.Concat(textBox1.Text.Reverse()).Replace(" ", "").Replace(" ", "");
102
-
103
60
  }
104
-
105
61
  ```
106
-
107
-
108
-
109
- 1行で書くこともできますが、やりすぎるとわかりにくくなります。
110
-
111
- ```C#
112
-
113
- private void button1_Click(object sender, RoutedEventArgs e)
114
-
115
- {
116
-
117
- textBox2.Text = string.Concat(textBox1.Text.Reverse()).Replace(" ", "").Replace(" ", "");
118
-
119
- }
120
-
121
- ```
122
-
123
-
124
62
 
125
63
  ---
126
64
 
127
-
128
-
129
65
  改行(`\r\n`)がある場合は、もうひと手間入れる必要がありますね。