Reverse a String in C#

Chandu yadav
Updated on 21-Jun-2020 15:54:35

612 Views

To reverse a string, use the Array. Reverse() method.We have set a method and passed the string value as “Henry” −public static string ReverseFunc(string str) {    char[] ch = str.ToCharArray();    Array.Reverse(ch);    return new string(ch); }In the above method, we have converted the string into character array −char[] ch = str.ToCharArray();Then the Reverse() method is used −Array.Reverse(ch);

String Format for DateTime in C#

Samual Sam
Updated on 21-Jun-2020 15:54:17

484 Views

Format DateTime using String.Format method.Let us see an example −Exampleusing System; static class Demo {    static void Main() {       DateTime d = new DateTime(2018, 2, 8, 12, 7, 7, 123);       Console.WriteLine(String.Format("{0:y yy yyy yyyy}", d));       Console.WriteLine(String.Format("{0:M MM MMM MMMM}", d));       Console.WriteLine(String.Format("{0:d dd ddd dddd}", d));    } }Above we have first set the DateTime class object −DateTime d = new DateTime(2018, 2, 8, 12, 7, 7, 123);To format, we have used the String.Format() method and displayed date in different formats.String.Format("{0:y yy yyy yyyy}", d) String.Format("{0:M MM MMM MMMM}", d) String.Format("{0:d dd ddd dddd}", d

Reverse Words in a Given String in C#

Chandu yadav
Updated on 21-Jun-2020 15:53:26

722 Views

Let’s say the following is the string −WelcomeAfter reversing the string, the words should be visible like −emocleWUse the reverse() method and try the following code to reverse words in a string −Exampleusing System; using System.Linq; class Demo {    static void Main() {       string str = "Welcome";       // reverse the string       string res = string.Join(" ", str.Split(' ').Select(s => new String(s.Reverse().ToArray())));       Console.WriteLine(res);    } }

Chash: Put Spaces Between Words Starting with Capital Letters

Arjun Thakur
Updated on 21-Jun-2020 15:52:44

5K+ Views

To place spaces in between words starting with capital letters, try the following example −Firstly, set the string.var str = "WelcomeToMyWebsite";As you can see above, our string isn’t having spaces before capital letter. To add it, use LINQ as we have done below −str = string.Concat(str.Select(x => Char.IsUpper(x) ? " " + x : x.ToString())).TrimStart(' ');The following is the complete code to place spaces between words beginning with capital letters −Exampleusing System; using System.Linq; class Demo {    static void Main() {       var str = "WelcomeToMyWebsite";       Console.WriteLine("Original String: "+str);     ... Read More

Add Item to ArrayList in C#

Samual Sam
Updated on 21-Jun-2020 15:51:25

241 Views

ArrayList is a non-generic type of collection in C# that dynamically resizes.Let us see how to initialize ArrayList in C# −ArrayList arr= new ArrayList();Add an item to an Array List −ArrayList arr1 = new ArrayList(); arr1.Add(30); arr1.Add(70);Let us see the complete example to implement ArrayList in C#. Here we have two array lists. The 2nd array list is appended to the first list.Exampleusing System; using System.Collections; public class MyClass {    public static void Main() {       ArrayList arr1 = new ArrayList();       arr1.Add(30);       arr1.Add(70);       ArrayList arr2 = ... Read More

Initializing HashSet in C#

Ankith Reddy
Updated on 21-Jun-2020 15:50:18

1K+ Views

To initialize a HashSet.var h = new HashSet(arr1);Above, we have set an array in the HashSet. The following is the array −string[] arr1 = {    "electronics",    "accessories”,    "electronics", };The following is an example showing how to implement HashSet in C# −Exampleusing System; using System.Collections.Generic; using System.Linq; class Program {    static void Main() {       string[] arr1 = {          "electronics",          "accessories”,          "electronics",       };       Console.WriteLine(string.Join(",", arr1));       // HashSet       var h = new HashSet(arr1);       // eliminates duplicate words       string[] arr2 = h.ToArray();       Console.WriteLine(string.Join(",", arr2));    } }

Final Keyword in C#

karthikeya Boyini
Updated on 21-Jun-2020 15:48:12

5K+ Views

Java has a final keyword, but C# does not have its implementation. For the same implementation, use the sealed keyword.With sealed, you can prevent overriding of a method. When you use sealed modifiers in C# on a method, then the method loses its capabilities of overriding. The sealed method should be part of a derived class and the method must be an overridden method.The following example won’t allow you to override the method display() because it has a sealed modifier for the ClassTwo derived class.ClassOne is our base class, whereas ClassTwo and ClassThree are derived classes −Exampleclass ClassOne {   ... Read More

Final Variables in C#

Samual Sam
Updated on 21-Jun-2020 15:47:04

5K+ Views

Java has a final keyword, but C# does not have its implementation. Use the sealed or readonly keyword in C# for the same implementation.The readonly would allow the variables to be assigned a value only once. A field marked "read-only", can only be set once during the construction of an object. It cannot be changed.Exampleclass Employee {    readonly int age;    Employee(int age) {       this.age = age;    }    void ChangeAge() {          //age = 27; // Compile error    } }Above, we have set the age field as readonly, which once assigned cannot be changed.

What is the HashSet Collection in C#

Chandu yadav
Updated on 21-Jun-2020 15:46:43

339 Views

HashSet in C# eliminates duplicate strings or elements in an array.In C#, it is an optimized set collection.Let us see an example to remove duplicate strings using C# HashSet −Exampleusing System; using System.Collections.Generic; using System.Linq; class Program {    static void Main() {       string[] arr1 = {          "one",          "two",          "two",          "one",          "three"       };       Console.WriteLine(string.Join(",", arr1));       // HashSet       var h = new HashSet(arr1);       // eliminates duplicate words       string[] arr2 = h.ToArray();       Console.WriteLine(string.Join(",", arr2));    } }To declare HashSet.var h = new HashSet

Assign Values to Arrays in C#

George John
Updated on 21-Jun-2020 15:45:59

162 Views

Declaring an array does not initialize the array in the memory. When the array variable is initialized, you can assign values to the array.Array is a reference type, so you need to use the new keyword to create an instance of the array. For example, assign the values like the following statement −double[] price = new double[5]; price[0] = 3245.50; price[1] = 1234.50; price[2] = 8765.50; price[3] = 5784.50; price[4] = 6576.50;We assigned five values above to the price array. You can assign values to the array at the time of declaration.double[] price = new double[5] {3245.50, 1234.50, 8765.50, 6576.50;};

Advertisements