- 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 34494 Articles for Programming
11K+ Views
To get the first 10 characters, use the substring() method.Let’s say the following is our string −string str = "Cricket is a religion in India!";Now to get the first 10 characters, set the value 10 in the substring() method as shown below −string res = str.Substring(0, 10);Let us see the complete code.Example Live Demousing System; public class Demo { public static void Main() { string str = "Cricket is a religion in India!"; string res = str.Substring(0, 10); Console.WriteLine(res); } }OutputCricket is
148 Views
To find the average values, firstly set an arrayLint[] myArr = new int[10] { 45, 23, 55, 15, 8, 4, 2, 5, 9, 14 };Now find the sum and divide it by the length of the array to get the average.int len = myArr.Length; int sum = 0; int average = 0; for (int i = 0; i < len; i++) { sum += myArr[i]; average = sum / len; }Let us see the complete code −Exampleusing System; public class Program { public static void Main() { int[] myArr = new int[10] { 45, 23, 55, 15, 8, 4, 2, 5, 9, 14 }; int len = myArr.Length; int sum = 0; int average = 0; for (int i = 0; i < len; i++) { sum += myArr[i]; } average = sum / len; Console.WriteLine("Sum = " + sum); Console.WriteLine("Average Of integer elements = " + average); } }
148 Views
The following is our integer array −int[] myArr = new int[6] { 8, 4, 2, 5, 9, 14 };Firstly, get the length of the array, and loop through the array to find the sum of the elements. After that, divide it with the length.int len = myArr.Length; int sum = 0; int average = 0; for (int i = 0; i < len; i++) { sum += myArr[i]; } average = sum / len;Here is the complete codeExample Live Demousing System; public class Program { public static void Main() { ... Read More
939 Views
Use the Replace() method to replace a character with asterisks.Let’s say our string is −string str = "dem* text";To replace it, use the Replace() method −str.Replace('*', 'o');Here is the complete code −Example Live Demousing System; public class Program { public static void Main() { string str = "dem* text"; Console.WriteLine("Initial string = " + str); string res = str.Replace('*', 'o'); // after replacing Console.WriteLine("After replacing asterisk = " + res.ToString()); } }OutputInitial string = dem* text After replacing asterisk = demo text
5K+ Views
Let’s say our string is −string str = "abcd$ef$gh";To replace the special character, use the Replace() method.string res = str.Replace('$', 'k');The following is the complete code to replace character from a string −Example Live Demousing System; public class Program { public static void Main() { string str = "abcd$ef$gh"; Console.WriteLine("Initial string = " + str); string res = str.Replace('$', 'k'); // after replacing Console.WriteLine("Replaced string = " + res.ToString()); } }OutputInitial string = abcd$ef$gh Replaced string = abcdkefkgh
1K+ Views
Set a string with duplicate words.string str = "One Two Three One";Above, you can see the word “One” comes twice.To remove dulicate words, you can try to run the following code in C# −Example Live Demousing System; using System.Linq; public class Program { public static void Main() { string str = "One Two Three One"; string[] arr = str.Split(' '); Console.WriteLine(str); var a = from k in arr orderby k select k; Console.WriteLine("After removing duplicate words..."); ... Read More
168 Views
Firstly, declare a HashSet and add elements −var names = new HashSet(); names.Add("Tim"); names.Add("John"); names.Add("Tom"); names.Add("Kevin");To remove an element, use RemoveWhere.names.RemoveWhere(x => x == "John");Let us see the complete example −Exampleusing System; using System.Collections.Generic; public class Program { public static void Main() { var names = new HashSet < string > (); names.Add("Tim"); names.Add("John"); names.Add("Tom"); names.Add("Kevin"); Console.WriteLine("Initial Set..."); foreach(var val in names) { Console.WriteLine(val); } names.RemoveWhere(x => x ... Read More
5K+ Views
Set the list.List < int > list = new List < int > (); list.Add(99); list.Add(49); list.Add(32);To get unique elements.List myList = list.Distinct().ToList();Here is the complete example to display unique values from a list.Example Live Demousing System; using System.Collections.Generic; using System.Linq; public class Demo { public static void Main() { List < int > list = new List < int > (); list.Add(55); list.Add(45); list.Add(55); list.Add(65); list.Add(73); list.Add(24); list.Add(65); Console.WriteLine("Initial List..."); ... Read More
1K+ Views
Let’s say you need to find the elements in the following list to be greater than 80.int[] arr = new int[] {55, 100, 87, 45};For that, loop until the array length. Here, res = 80 i.e. the given element.for (int i = 0; i < arr.Length; i++) { if(arr[i] res) { Console.WriteLine(arr[i]); } } } } }
471 Views
Declare a list and add elements.List list = new List(); list.Add(50); list.Add(90); list.Add(50); list.Add(100);Now, use Distinct() method to get the unique elements only.List myList = list.Distinct().ToList();The following is the complete code to remove duplicate elements from a List −Example Live Demousing System; using System.Collections.Generic; using System.Linq; public class Demo { public static void Main() { List < int > list = new List < int > (); list.Add(50); list.Add(90); list.Add(50); list.Add(100); Console.WriteLine("Initial List..."); foreach(int a in list) ... Read More