Skip to content

Commit 3d365a4

Browse files
authored
Update 4-exercise-string-methods-padding.md
1 parent bb57ca0 commit 3d365a4

File tree

1 file changed

+101
-29
lines changed

1 file changed

+101
-29
lines changed

learn-pr/wwl-language/csharp-format-strings/includes/4-exercise-string-methods-padding.md

Lines changed: 101 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -27,21 +27,36 @@ The `PadLeft()` method adds blank spaces to the left-hand side of the string so
2727
1. Delete or use the line comment operator `//` to comment out all of the code from the previous exercises.
2828

2929
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+
```
3135

3236
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.
3439

3540
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.
3743

3844
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+
```
4254

4355
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+
```
4560

4661
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.
4762

@@ -54,10 +69,18 @@ You can also call a second *overloaded* version of the method and pass in whatev
5469
1. Delete or use the line comment operator `//` to comment out all of the code from the previous step.
5570
5671
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+
```
5877

5978
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+
```
6184

6285
Now, apply this newfound knowledge to another real world scenario.
6386

@@ -74,13 +97,22 @@ To get started, print the Payment ID in the first six columns. You pick some ran
7497
1. Delete or use the line comment operator `//` to comment out all of the code from the previous step.
7598

7699
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.
81110

82111
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+
```
84116

85117
There are three blank spaces to the right that not visible. You'll confirm that they exist in the next step.
86118

@@ -89,14 +121,28 @@ There are three blank spaces to the right that not visible. You'll confirm that
89121
Next, you add a fictitious Payee Name, padding it appropriately.
90122

91123
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+
```
97140

98141
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+
```
100146

101147
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.
102148

@@ -105,12 +151,24 @@ Again, there are quite a few blank spaces after the Payee's Name. Also, there ar
105151
Next, add a fictitious Payment Amount and make sure to use `PadLeft()` to right-align the output.
106152

107153
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+
```
111166

112167
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+
```
114172

115173
This output is pretty close to what you understood the legacy system maintainers were looking for.
116174

@@ -123,12 +181,26 @@ Console.WriteLine("1234567890123456789012345678901234567890");
123181
```
124182

125183
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+
```
129197

130198
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+
```
132204

133205
Success!
134206

@@ -140,4 +212,4 @@ There's a few important takeaways from this unit.
140212
- The `PadLeft()` and `PadRight()` methods add white space (or optionally, another character) to the total length of a string.
141213
- Use `PadLeft()` to right-align a string.
142214
- 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

Comments
 (0)