From 27eb1c97177e82aebb8e3d9b7556a32a7d301946 Mon Sep 17 00:00:00 2001 From: krzysztofmmm Date: Tue, 9 Jan 2024 12:47:26 +0100 Subject: [PATCH] Make additional tasks --- csharp-fundamentals-strings.Main/Extension.cs | 27 ++++++++++--------- 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/csharp-fundamentals-strings.Main/Extension.cs b/csharp-fundamentals-strings.Main/Extension.cs index ac3b7b2..6711d12 100644 --- a/csharp-fundamentals-strings.Main/Extension.cs +++ b/csharp-fundamentals-strings.Main/Extension.cs @@ -1,8 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +using System.Text; namespace csharp_fundamentals_strings.Main { @@ -43,11 +39,11 @@ public StringBuilder one() // 1. Using the sb variable above, add "Hello, world!" to the StringBuilder // WRITE YOUR CODE BETWEEN THIS LINE... - + sb.Append("Hello, world!"); + return sb; // ...AND THIS LINE - return sb; } public StringBuilder two() @@ -58,11 +54,13 @@ public StringBuilder two() // 2. After adding the message, use an appropriate StringBuilder method to reverse it // WRITE YOUR CODE BETWEEN THIS LINE... - + sb.Append("Hello, world!"); + char[] charArray = sb.ToString().ToCharArray(); + Array.Reverse(charArray); + return new StringBuilder(new string(charArray)); // ...AND THIS LINE - return sb; } public StringBuilder three() @@ -73,7 +71,12 @@ public StringBuilder three() // 2. After adding the message, remove the comma. // WRITE YOUR CODE BETWEEN THIS LINE... - + sb.Append("Hello, world!"); + int index = sb.ToString().IndexOf(","); + if(index >= 0) + { + sb.Remove(index , 1); + } // ...AND THIS LINE @@ -88,11 +91,11 @@ public StringBuilder four() // 2. After adding the message, replace the word "world" with the word "C#" // WRITE YOUR CODE BETWEEN THIS LINE... - + sb.Append("Hello, world!"); + return sb.Replace("world" , "C#"); // ...AND THIS LINE - return sb; } } }