How to split a string using regular expressions in C#?

George John
Published on 18-Sep-2018 14:06:00
To split a string suing regular expression, use the Regex.split.Let’s say our string is −string str = "Hello\r\nWorld";Now use Regex.split to split the string as shown below −tring[] res = Regex.Split(str, "\r\n");The following is the complete code to split a string using Regular Expression in C#.Example Live Demousing System; using System.Text.RegularExpressions; ... Read More

How to split a string into elements of a string array in C#?

karthikeya Boyini
Published on 18-Sep-2018 14:05:42
Set the string you want to split.string str = "Hello World!";Use the split() method to split the string into separate elements.string[] res = str.Split(' ');The following is the complete code to split a string into elements of a string array in C#.Example Live Demousing System; class Demo {    static void ... Read More

How to sort a list of dictionaries by values of dictionaries in C#?

Samual Sam
Published on 18-Sep-2018 14:02:27
Set the list of dictionaries with keys and values.var d = new Dictionary<string, int>(); d.Add("Zack", 0); d.Add("Akon", 3); d.Add("Jack", 2); d.Add("Tom", 1);Get and sort the keys.var val = d.Keys.ToList(); val.Sort();You can try to run the following code to sort a list of dictionaries by values.Example Live Demousing System; using System.Collections.Generic; using ... Read More

How do you access jagged arrays in C#?

Ankith Reddy
Published on 18-Sep-2018 14:02:44
A Jagged array is an array of arrays. You can declare a jagged array named scores of type int as.int [][] points;Let us now see how to initialize it.int[][] points = new int[][]{new int[]{10, 5}, new int[]{30, 40}, new int[]{70, 80}, new int[]{ 60, 70 }};Access the jagged array element ... Read More

How to select a random element from a C# list?

Arjun Thakur
Published on 18-Sep-2018 13:58:14
Firstly, set a list in C#.var list = new List<string>{ "one", "two", "three", "four"};Now get the count of the elements and display randomly.int index = random.Next(list.Count); Console.WriteLine(list[index]);To select a random element from a list in C#, try to run the following code −Example Live Demousing System; using System.Collections.Generic; namespace Demo { ... Read More

How to use WriteLine() method of Console class in C#?

Chandu yadav
Published on 18-Sep-2018 13:54:29
WriteLine() is a method of the Console class defined in the System namespaceThis statement causes the message "Welcome!" to be displayed on the screen as shown below −Example Live Demousing System; namespace Demo {    class Test {       static void Main(string[] args) {          Console.WriteLine("Welcome!"); ... Read More

How to use XmlSerializer in C#?

karthikeya Boyini
Published on 18-Sep-2018 13:58:22
Serialization/ De-serialization allow communication with another application by sending and receiving data. With XmlSerializer, you can control how objects are encoded into XML. To perform XML Serialization, you need the following two classes − StreamWriter class XmlSerializer class Call the Serialize method with the parameters of the StreamWriter ... Read More

How to use Try/catch blocks in C#?

Samual Sam
Published on 18-Sep-2018 13:51:27
Exceptions provide a way to transfer control from one part of a program to another. C# exception handling is built upon four keywords: try, catch, finally, and throw.try − A try block identifies a block of code for which particular exceptions is activated. It is followed by one or more ... Read More

How to use the ToString() method of array in C#?

George John
Published on 18-Sep-2018 13:30:51
The ToString() method returns a string that represents the current object.In the below example, we have used the ToString() method with another Array class method.arr.GetLowerBound(0).ToString()Example Live Demousing System; using System.Collections.Generic; using System.Linq; using System.Text; namespace lower {    class Program {       static void Main(string[] args) {     ... Read More

How to use the Compare method of the string class in C#?

karthikeya Boyini
Published on 18-Sep-2018 13:30:21
The Compare method compares two specified string objects and returns an integer that indicates their relative position in the sort order.Firstly, set the strings.string str1 = "Jack"; string str2 = "Mac";Now compare the strings using the Compare() method and if the comparison results 0, then it would mean the strings ... Read More
Advertisements