To split a string suing regular expression, use the Regex.split.Let’s say our string is −string str = "Hello\rWorld";Now use Regex.split to split the string as shown below −tring[] res = Regex.Split(str, "\r");The following is the complete code to split a string using Regular Expression in C#.Example Live Demousing System; using System.Text.RegularExpressions; class Demo { static void Main() { string str = "Hello\rWorld"; string[] res = Regex.Split(str, "\r"); foreach (string word in res) { Console.WriteLine(word); } } }OutputHello World
Delimiters are the commas that you can see in the below string.string str = "Welcome, to, New York";Now set the delimiter separately.char[] newDelimiter = new char[] { ', ' };Use theSplit() method to split the string considering the delimiter as the parameter.str.Split(newDelimiter, StringSplitOptions.None);To split a string with a string deli meter, try to run the following code −Example Live Demousing System; class Program { static void Main() { string str = "Welcome, to, New York"; char[] newDelimiter = new char[] { ', ' }; string[] arr = str.Split(newDelimiter, StringSplitOptions.None); ... Read More
A 2-dimensional array can be thought of as a table, which has x number of rows and y number of columns.An element in 2-dimensional array is accessed by using the subscripts. That is, row index and column index of the array.int x = a[1, 1]; Console.WriteLine(x);Let us see an example that shows how to access elements from two-dimensional array.Example Live Demousing System; namespace Demo { class MyArray { static void Main(string[] args) { /* an array with 5 rows and 2 columns*/ int[, ] a = new int[5, 2] ... Read More
To call a C# method recursively, you can try to run the following code. Here, Factorial of a number is what we are finding using a recursive function display().If the value is 1, it returns 1 since Factorial is 1.if (n == 1) return 1;If not, then the recursive function will be called for the following iterations if 1you want the value of 5!Interation1: 5 * display(5 - 1); Interation2: 4 * display(4 - 1); Interation3: 3 * display(3 - 1); Interation4: 4 * display(2 - 1);The following is the complete code to call a C# method recursively.Example Live Demousing System; ... Read More
Set the unsorted array first.int[] list = {87, 45, 56, 22, 84, 65};Now use a nested for loop to sort the list, which is passed to a function.for(int i=0; i< arr.Length; i++) { for(int j=i+1; j=arr[j]) { temp=arr[j]; arr[j]=arr[i]; arr[i]=temp; } } Console.Write(arr[i] + " "); }The following is the complete code to sort one-dimensional array in ascending order using non-static method.Example Live Demousing System; namespace Demo { public class MyApplication { public static void Main(string[] args) { ... Read More
Declare Jagged ArrayA Jagged array is an array of arrays. You can declare a jagged array named scores of type int as −int [][] points;Initialize Jagged ArrayLet 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 ElementAccess the jagged array element as.points[i][j]);The following is the complete example showing how to work with jagged arrays in C#.Example Live Demousing System; namespace ArrayApplication { class MyArray { static void Main(string[] args) { int[][] points = new int[][]{new ... Read More
The Sort() method sorts the elements in an entire one-dimensional Array using the IComparable implementation of each element of the Array.Set the array.int[] list = { 22, 12, 65, 9};Use the Sort() method to sort the array.Array.Sort(list);The following is an example to learn how to work with the Sort() method.Example Live Demousing System; namespace Demo { class Program { static void Main(string[] args) { int[] list = { 22, 12, 65, 9}; Console.Write("Original Array: "); foreach (int i in list) { ... Read More
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) { Array arr = Array.CreateInstance(typeof(String), 3); arr.SetValue("One", 0); arr.SetValue("Two", 1); Console.WriteLine("Lower Bound {0}",arr.GetLowerBound(0).ToString()); Console.ReadLine(); } } }OutputLower Bound 0
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!"); Console.ReadKey(); } } }OutputWelcome!To display a char array using the Console.WriteLine.Example Live Demousing System; namespace Demo { class Test { static void Main(string[] args) { char[] arr = new char[] { 'W', 'e'}; Console.WriteLine(arr); Console.ReadKey(); } } }OutputWe
Firstly, set a list in C#.var list = new List{ "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 { class Program { static void Main(string[] args) { var random = new Random(); var list = new List{ "one","two","three","four"}; int index = random.Next(list.Count); Console.WriteLine(list[index]); } } }Outputthree
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP