@@ -27,21 +27,36 @@ The `PadLeft()` method adds blank spaces to the left-hand side of the string so
27
27
1 . Delete or use the line comment operator ` // ` to comment out all of the code from the previous exercises.
28
28
29
29
1 . Update your code in the Visual Studio Code Editor as follows:
30
- ``` csharp string input = "Pad this"; Console.WriteLine(input.PadLeft(12)); ```
30
+
31
+ ``` csharp
32
+ string input = " Pad this" ;
33
+ Console .WriteLine (input .PadLeft (12 ));
34
+ ```
31
35
32
36
1 . On the Visual Studio Code ** File ** menu , select ** Save ** .
33
- Save the Program.cs file before building or running the code.
37
+
38
+ Save the Program .cs file before building or running the code .
34
39
35
40
1 . In the EXPLORER panel , to open a Terminal at your TestProject folder location , right - click ** TestProject ** , and then select ** Open in Integrated Terminal ** .
36
- A Terminal panel should open, and should include a command prompt showing that the Terminal is open to your TestProject folder location.
41
+
42
+ A Terminal panel should open , and should include a command prompt showing that the Terminal is open to your TestProject folder location .
37
43
38
44
1. At the Terminal command prompt , to run your code , type ** dotnet run ** and then press Enter .
39
- > [ !NOTE] > If you see a message saying "Couldn't find a project to run", ensure that the Terminal command prompt displays the expected TestProject folder location. For example: ` C:\Users\someuser\Desktop\csharpprojects\TestProject> `
40
- When you run the code, you observe four characters prefixed to the left of the string bring the length to 12 characters long.
41
- ``` Output Pad this ```
45
+
46
+ > [!NOTE ]
47
+ > If you see a message saying " Couldn't find a project to run" , ensure that the Terminal command prompt displays the expected TestProject folder location . For example : `C : \Users \someuser \Desktop \csharpprojects \TestProject > `
48
+
49
+ When you run the code , you observe four characters prefixed to the left of the string bring the length to 12 characters long .
50
+
51
+ ```Output
52
+ Pad this
53
+ ```
42
54
43
55
1 . To add space or characters to the right side of your string , use the `PadRight ()` method instead . 1 . Update your code in the Visual Studio Code Editor as follows :
44
- ``` csharp Console.WriteLine(input.PadRight(12)); ```
56
+
57
+ ```csharp
58
+ Console .WriteLine (input .PadRight (12 ));
59
+ ```
45
60
46
61
1 . Save your code file , and then use Visual Studio Code to run your code . You won 't observe any characters added to the end of the string, but they' re there .
47
62
@@ -54,10 +69,18 @@ You can also call a second *overloaded* version of the method and pass in whatev
54
69
1 . Delete or use the line comment operator `// ` to comment out all of the code from the previous step.
55
70
56
71
1 . Update your code in the Visual Studio Code Editor as follows :
57
- ``` csharp Console.WriteLine(input.PadLeft(12, '-')); Console.WriteLine(input.PadRight(12, '-')); ```
72
+
73
+ ```csharp
74
+ Console .WriteLine (input .PadLeft (12 , '-' ));
75
+ Console .WriteLine (input .PadRight (12 , '-' ));
76
+ ```
58
77
59
78
1 . Save your code file , and then use Visual Studio Code to run your code . You should see four dashes prefixing the left of the string that is 12 characters long .
60
- ``` Output ----Pad this Pad this---- ```
79
+
80
+ ```Output
81
+ ----Pad this
82
+ Pad this ----
83
+ ```
61
84
62
85
Now , apply this newfound knowledge to another real world scenario .
63
86
@@ -74,13 +97,22 @@ To get started, print the Payment ID in the first six columns. You pick some ran
74
97
1. Delete or use the line comment operator `//` to comment out all of the code from the previous step.
75
98
76
99
1. Update your code in the Visual Studio Code Editor as follows:
77
- ``` csharp string paymentId = "769C";
78
- var formattedLine = paymentId .PadRight (6 );
79
- Console .WriteLine (formattedLine ); ```
80
- Reuse the `formattedLine ` variable to build the output string .
100
+
101
+ ```csharp
102
+ string paymentId = "769C";
103
+
104
+ var formattedLine = paymentId .PadRight (6 );
105
+
106
+ Console .WriteLine (formattedLine );
107
+ ```
108
+
109
+ Reuse the `formattedLine ` variable to build the output string .
81
110
82
111
1 . Save your code file , and then use Visual Studio Code to run your code . You should see the following output :
83
- ```Output 769 ```
112
+
113
+ ```Output
114
+ 769
115
+ ```
84
116
85
117
There are three blank spaces to the right that not visible . You 'll confirm that they exist in the next step.
86
118
@@ -89,14 +121,28 @@ There are three blank spaces to the right that not visible. You'll confirm that
89
121
Next , you add a fictitious Payee Name , padding it appropriately .
90
122
91
123
1 . Update your code in the Visual Studio Code Editor as follows :
92
- ```csharp string paymentId = " 769" ; string payeeName = " Mr. Stephen Ortega" ;
93
- var formattedLine = paymentId .PadRight (6 ); formattedLine += payeeName .PadRight (24 );
94
- Console .WriteLine (formattedLine ); ```
95
- The `+= ` operator performs a string concatenation , taking the previous value of the variable `formattedLine ` and adding the new value to it . It 's a shortened equivalent the following code example:
96
- ```csharp formattedLine = formattedLine + payeeName .PadRight (24 ); ```
124
+
125
+ ```csharp
126
+ string paymentId = " 769" ;
127
+ string payeeName = " Mr. Stephen Ortega" ;
128
+
129
+ var formattedLine = paymentId .PadRight (6 );
130
+ formattedLine += payeeName .PadRight (24 );
131
+
132
+ Console .WriteLine (formattedLine );
133
+ ```
134
+
135
+ The `+= ` operator performs a string concatenation , taking the previous value of the variable `formattedLine ` and adding the new value to it . It 's a shortened equivalent the following code example:
136
+
137
+ ```csharp
138
+ formattedLine = formattedLine + payeeName .PadRight (24 );
139
+ ```
97
140
98
141
1 . Save your code file , and then use Visual Studio Code to run your code . You should see the following output :
99
- ```Output 769 Mr . Stephen Ortega ```
142
+
143
+ ```Output
144
+ 769 Mr . Stephen Ortega
145
+ ```
100
146
101
147
Again , there are quite a few blank spaces after the Payee 's Name. Also, there are three blank spaces after the Payment ID from Step 1.
102
148
@@ -105,12 +151,24 @@ Again, there are quite a few blank spaces after the Payee's Name. Also, there ar
105
151
Next , add a fictitious Payment Amount and make sure to use `PadLeft ()` to right - align the output .
106
152
107
153
1 . Update your code in the Visual Studio Code Editor as follows :
108
- ```csharp string paymentId = " 769" ; string payeeName = " Mr. Stephen Ortega" ; string paymentAmount = " $5,000.00" ;
109
- var formattedLine = paymentId .PadRight (6 ); formattedLine += payeeName .PadRight (24 ); formattedLine += paymentAmount .PadLeft (10 );
110
- Console .WriteLine (formattedLine ); ```
154
+
155
+ ```csharp
156
+ string paymentId = " 769" ;
157
+ string payeeName = " Mr. Stephen Ortega" ;
158
+ string paymentAmount = " $5,000.00" ;
159
+
160
+ var formattedLine = paymentId .PadRight (6 );
161
+ formattedLine += payeeName .PadRight (24 );
162
+ formattedLine += paymentAmount .PadLeft (10 );
163
+
164
+ Console .WriteLine (formattedLine );
165
+ ```
111
166
112
167
1 . Save your code file , and then use Visual Studio Code to run your code . You should see the following output :
113
- ```Output 769 Mr . Stephen Ortega $5 ,000 . 00 ```
168
+
169
+ ```Output
170
+ 769 Mr . Stephen Ortega $5 ,000 . 00
171
+ ```
114
172
115
173
This output is pretty close to what you understood the legacy system maintainers were looking for .
116
174
@@ -123,12 +181,26 @@ Console.WriteLine("1234567890123456789012345678901234567890");
123
181
```
124
182
125
183
1 . Update your code in the Visual Studio Code Editor as follows:
126
- ``` csharp string paymentId = "769"; string payeeName = "Mr. Stephen Ortega"; string paymentAmount = "$5,000.00";
127
- var formattedLine = paymentId .PadRight (6 ); formattedLine += payeeName .PadRight (24 ); formattedLine += paymentAmount .PadLeft (10 );
128
- Console .WriteLine (" 1234567890123456789012345678901234567890" ); Console .WriteLine (formattedLine ); ```
184
+
185
+ ``` csharp
186
+ string paymentId = " 769" ;
187
+ string payeeName = " Mr. Stephen Ortega" ;
188
+ string paymentAmount = " $5,000.00" ;
189
+
190
+ var formattedLine = paymentId .PadRight (6 );
191
+ formattedLine += payeeName .PadRight (24 );
192
+ formattedLine += paymentAmount .PadLeft (10 );
193
+
194
+ Console .WriteLine (" 1234567890123456789012345678901234567890" );
195
+ Console .WriteLine (formattedLine );
196
+ ```
129
197
130
198
1 . Save your code file , and then use Visual Studio Code to run your code . You should see the following output , that you can send off to the maintainers of the legacy system to confirm the new integration works correctly :
131
- ```Output 1234567890123456789012345678901234567890 769 Mr . Stephen Ortega $5 ,000 . 00 ```
199
+
200
+ ```Output
201
+ 1234567890123456789012345678901234567890
202
+ 769 Mr . Stephen Ortega $5 ,000 . 00
203
+ ```
132
204
133
205
Success !
134
206
@@ -140,4 +212,4 @@ There's a few important takeaways from this unit.
140
212
- The `PadLeft ()` and `PadRight ()` methods add white space (or optionally , another character ) to the total length of a string .
141
213
- Use `PadLeft ()` to right - align a string .
142
214
- Some methods are overloaded , meaning they have multiple versions of the method with different arguments that affect their functionality .
143
- - The `+= ` operator concatenates a new string on the right to the existing string on the left .
215
+ - The `+= ` operator concatenates a new string on the right to the existing string on the left .
0 commit comments