- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Found 34488 Articles for Programming
2K+ Views
To swap characters of a string, use the Select method.Firstly, let’s say our string is −string str = "PQRQP";Now, you need to swap every occurrence of P with Q and Q with P −str.Select(a=> a == 'P' ? 'Q' : (a=='Q' ? 'P' : a)).ToArray();The above replaces the characters.Let us see the compelet code −Example Live Demousing System; using System.Linq; public class Program { public static void Main() { string str = "PQRQP"; var res= str.Select(a=> a == 'P' ? 'Q' : (a=='Q' ? 'P' : a)).ToArray(); str = new String(res); Console.WriteLine(str); } }OutputQPRPQ
591 Views
To format a string, first set the value −int value = 55;Now to format the integer, use ToString and let’s say we need to set it for three places −value.ToString("000");The following is the complete code −Example Live Demousing System; public class Program { public static void Main() { int value = 55; string res = value.ToString("000"); Console.WriteLine(res); } }Output055
199 Views
To add padding on the right to a string −const string format = "{0,10}";Now add it to the string −string str1 = string.Format(format, "Marks","Subject");Let us see the complete code −Example Live Demousing System; public class Program { public static void Main() { // set right padding const string format = "{0,10}"; string str1 = string.Format(format, "Marks","Subject"); string str2 = string.Format(format, "95","Maths"); Console.WriteLine(str1); Console.WriteLine(str2); } }OutputMarks 95
246 Views
Use the String.Fornt to format a string using %.String.Format format controls in C# also includes percentage (%) This multiplies the value by 100 and appends a percentage sign.Let’s say our value is −double val = .322;Now, use String.Format and format −string.Format("string = {0:0.0%}", val);The following is an example −Example Live Demousing System; public class Program { public static void Main() { double val = .322; string res = string.Format("string = {0:0.0%}", val); Console.WriteLine(res); } }Outputstring = 32.2%
806 Views
With C#, you can easily format content and add padding to it.To add padding −const string format = "{0,-5} {1,5}";Now, add the padding to the strings −string str1 = string.Format(format, "Rank","Student"); string str2 = string.Format(format, "2","Tom");Let us see the complete code −Example Live Demousing System; using System.Collections.Generic; public class Program { public static void Main() { // set padding const string format = "{0,-5} {1,5}"; string str1 = string.Format(format, "Rank","Student"); string str2 = string.Format(format, "2","Tom"); Console.WriteLine(str1); Console.WriteLine(str2); } }OutputRank Student 2 Tom
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
459 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
410 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
555 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
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