Found 34494 Articles for Programming

How to find the first 10 characters of a string in C#?

karthikeya Boyini
Updated on 22-Jun-2020 09:56:07

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

How to find the Average Values of all the Array Elements in C#?

Arjun Thakur
Updated on 22-Jun-2020 09:55:47

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);    } }

How to find the average of elements of an integer array in C#?

Samual Sam
Updated on 22-Jun-2020 09:56:58

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

C# Program to replace a character with asterisks in a sentence

Ankith Reddy
Updated on 22-Jun-2020 09:57:18

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

C# Program to replace a special character from a String

karthikeya Boyini
Updated on 22-Jun-2020 09:57:38

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

C# program to remove all duplicates words from a given sentence

George John
Updated on 22-Jun-2020 09:44:04

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

C# program to remove an item from Set

Chandu yadav
Updated on 22-Jun-2020 09:45:15

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

C# program to print unique values from a list

Samual Sam
Updated on 22-Jun-2020 09:44:45

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

C# program to check if all the values in a list that are greater than a given value

Arjun Thakur
Updated on 22-Jun-2020 09:47:12

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]);             }          }       }    } }

C# program to remove duplicate elements from a List

karthikeya Boyini
Updated on 22-Jun-2020 09:46:18

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

Advertisements