Found 34494 Articles for Programming

String slicing in C# to rotate a string

George John
Updated on 22-Jun-2020 13:08:40

313 Views

Let’s say our string is −var str = "welcome";Use the substring() method and the following, if you want to rotate only some characters. Here, we are rotating only 2 characters −var res = str.Substring(1, str.Length - 1) + str.Substring(0, 2);The following is the complete code −Example Live Demousing System; public class Program {    public static void Main() {       var str = "welcome";       Console.WriteLine("Original String = "+str);       var res = str.Substring(1, str.Length - 1) + str.Substring(0, 2);       Console.WriteLine("Rotating two characters in the String: "+res);    } }OutputOriginal String = welcome Rotating two characters in the String: elcomewe

Merge two sorted arrays in C#

radhakrishna
Updated on 22-Jun-2020 13:09:18

454 Views

To merge two sorted arrays, firstly set two sorted arrays −int[] array1 = { 1, 2 }; int[] array2 = { 3, 4 };Now, add it to a list and merge −var list = new List(); for (int i = 0; i < array1.Length; i++) {    list.Add(array1[i]);    list.Add(array2[i]); }Use the ToArray() method to convert back into an array −int[] array3 = list.ToArray();The following is the complete code −Example Live Demousing System; using System.Collections.Generic; public class Program {    public static void Main() {       int[] array1 = { 1, 2 };       int[] array2 ... Read More

UnionWith Method in C#

Chandu yadav
Updated on 22-Jun-2020 13:09:51

407 Views

Use the UnionWith method in C# to get the union of i.e. unique lements from two collections.Let’s say the following are our Dictionary −Dictionary < string, int > dict1 = new Dictionary < string, int > (); dict1.Add("pencil", 1); dict1.Add("pen", 2); Dictionary < string, int > dict2 = new Dictionary < string, int > (); dict2.Add("pen", 3);Now, use HashSet and UnionWith to get the union −HashSet < string > hSet = new HashSet < string > (dict1.Keys); hSet.UnionWith(dict2.Keys);The following is the complete code −Example Live Demousing System; using System.Collections.Generic; public class Program {    public static void Main() {   ... Read More

Union Method in C#

Arjun Thakur
Updated on 22-Jun-2020 12:59:22

552 Views

The Union method gets the unique elements from both the lists.Let us set two lists −var list1 = new List{12, 65, 88, 45}; var list2 = new List{40, 34, 65};Now get the union of both the lists −var res = list1.Union(list2);The following is the example −Example Live Demousing System.Collections.Generic; using System.Linq; using System; public class Demo {    public static void Main() {       // two lists       var list1 = new List{12, 65, 88, 45};       var list2 = new List{40, 34, 65};       // finding union       var res = list1.Union(list2);       foreach(int i in res) {          Console.WriteLine(i);       }    } }Output12 65 88 45 40 34

Concat Method in C#

Ankith Reddy
Updated on 22-Jun-2020 12:59:51

162 Views

To concat lists in C#, ue the Concat method.The following is the list −var list1 = new List{12, 40}; var list2 = new List{98, 122, 199, 230};Here is the Concat method −var res = list1.Concat(list2);The following is the example to work with Concat method −Example Live Demousing System.Collections.Generic; using System.Linq; using System; public class Demo {    public static void Main() {       // two lists       var list1 = new List{12, 40};       var list2 = new List{98, 122, 199, 230};           // concat       var res = list1.Concat(list2);       foreach(int i in res) {          Console.WriteLine(i);       }    } }Output12 40 98 122 199 230

C# program to find common values from two or more Lists

George John
Updated on 22-Jun-2020 13:00:17

249 Views

Create more than one list −// two lists var list1 = new List{3, 4}; var list2 = new List{1, 2, 3};Now, use the Intersect() method to get the common values −var res = list1.Intersect(list2);The following is the complete code −Example Live Demousing System.Collections.Generic; using System.Linq; using System; public class Demo {    public static void Main() {       // two lists       var list1 = new List{3, 4};       var list2 = new List{1, 2, 3};       // common values       var res = list1.Intersect(list2);       foreach(int i in res) {          Console.WriteLine(i);       }    } }Output3

C# program to concat two or more Lists

Chandu yadav
Updated on 22-Jun-2020 13:00:54

165 Views

Set three lists −// three lists var list1 = new List{3, 4}; var list2 = new List{1, 2, 3}; var list3 = new List{2, 5, 6};Now, use the Concat mthod to concat the above lists −var res1 = list1.Concat(list2); var res2 = res1.Concat(list3);Here is the complete code −Example Live Demousing System.Collections.Generic; using System.Linq; using System; public class Demo {    public static void Main() {       // three lists       var list1 = new List{3, 4};       var list2 = new List{1, 2, 3};       var list3 = new List{2, 5, 6};       // concat       var res1 = list1.Concat(list2);       var res2 = res1.Concat(list3);       foreach(int i in res2) {          Console.WriteLine(i);       }    } }Output3 4 1 2 3 2 5 6

C# program to find Union of two or more Lists

Arjun Thakur
Updated on 22-Jun-2020 13:01:22

900 Views

Firstly, create lists −//three lists var list1 = new List{3, 4, 5}; var list2 = new List{1, 2, 3, 4, 5}; var list3 = new List{5, 6, 7, 8};Use the union method to get the union of list1 and list2 −var res1 = list1.Union(list2); var res2 = res1.Union(list3);The following is the complete code −Example Live Demousing System.Collections.Generic; using System.Linq; using System; public class Demo {    public static void Main() {       //three lists       var list1 = new List{3, 4, 5};       var list2 = new List{1, 2, 3, 4, 5};       var list3 = new List{5, 6, 7, 8};       // finding union       var res1 = list1.Union(list2);       var res2 = res1.Union(list3);       foreach(int i in res2) {          Console.WriteLine(i);       }    } }Output3 4 5 1 2 6 7 8

C# program to find Union of two or more Dictionaries

Chandu yadav
Updated on 22-Jun-2020 13:01:54

507 Views

Firstly, set both the dictionaries −Dictionary < string, int > dict1 = new Dictionary < string, int > (); dict1.Add("water", 1); dict1.Add("food", 2); Dictionary < string, int > dict2 = new Dictionary < string, int > (); dict2.Add("clothing", 3); dict2.Add("shelter", 4);Now, create HashSet and use UnionsWith() method to find the union between the above two Dictionaries −HashSet < string > hSet = new HashSet < string > (dict1.Keys); hSet.UnionWith(dict2.Keys);The following is the complete code −Example Live Demousing System; using System.Collections.Generic; public class Program {    public static void Main() {       Dictionary < string, int > dict1 = ... Read More

How to check if two Strings are anagrams of each other using C#?

Samual Sam
Updated on 22-Jun-2020 13:02:55

1K+ Views

Under anagram, another string would have the same characters present in the first string, but the order of characters can be different.Here, we are checking the following two strings −string str1 = "silent"; string str2 = "listen";Convert both the strings into character array −char[] ch1 = str1.ToLower().ToCharArray(); char[] ch2 = str2.ToLower().ToCharArray();Now, sort them −Array.Sort(ch1); Array.Sort(ch2);After sorting, convert them to strings −string val1 = new string(ch1); string val2 = new string(ch2);Compare both the strings for equality. If both are equal, that would mean they are anagrams.The following is the code −Example Live Demousing System; public class Demo {    public static ... Read More

Advertisements