If you want to remove dots(.), hyphens(-), spaces, braces ‘(‘ and ‘)’ from a string in C# then you can use any solution given below.
string str = "A sample string having many . and ......It also has a hyphen (-) and too Many hyphens(------) Enjoy!!";
// Solution1: Use of String concat Function and Lambda Extension Method
var solution1 = string.Concat(str.Where(i => !new[] { '.', ' ', '-', '(', ')' }.Contains(i)));
Response.Write(solution1);
// Solution2: Use of Regular Expression. (Make sure you add using System.Text.RegularExpressions; on top of your page before using below solution2)
var solution2 = Regex.Replace(str, "[ ().-]+", "");
Response.Write(solution2);
// Solution3 : Use of Lambda and Extension Methods
var solution3 = str.ToCharArray().Where(i => i != ' ' && i != '-' && i != '.' && i != '(' && i != ')' ).Aggregate(" ", (a, b) => a + b);
Response.Write(solution3);
The .NET Framework allows developers to use the same set of skills to rapidly buid great applications for the web, windows, services and more.
Remove Duplicates From List in C#
To Remove Duplicates From List can be done by many different ways. You can use Distinct extension method to remove duplicates.
Below program that removes duplicates from the list in C#.
using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
static void Main()
{
// List having duplicate integer elements.
List<int> list = new List<int>();
list.Add(7);
list.Add(2);
list.Add(5);
list.Add(5);
list.Add(2);
list.Add(3);
list.Add(7);
// Get distinct elements and convert again into a list.
List<int> distinct = list.Distinct().ToList();
foreach (int value in distinct)
{
Console.WriteLine("Distinct : {0}", value);
}
}
}
OR
You can use LINQ to object to remove duplicates from list,
using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
static void Main()
{
// List having duplicate string elements.
List<string> list = new List<string>();
list.Add("A");
list.Add("A");
list.Add("C");
list.Add("D");
list.Add("B");
list.Add("A");
list.Add("E");
list.Add("E");
// Get distinct elements.
var distinct = (from item in list orderby item select item).Distinct();
foreach (string value in distinct)
{
Console.WriteLine("Distinct : {0}", value);
}
}
}
Below program that removes duplicates from the list in C#.
using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
static void Main()
{
// List having duplicate integer elements.
List<int> list = new List<int>();
list.Add(7);
list.Add(2);
list.Add(5);
list.Add(5);
list.Add(2);
list.Add(3);
list.Add(7);
// Get distinct elements and convert again into a list.
List<int> distinct = list.Distinct().ToList();
foreach (int value in distinct)
{
Console.WriteLine("Distinct : {0}", value);
}
}
}
OR
You can use LINQ to object to remove duplicates from list,
using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
static void Main()
{
// List having duplicate string elements.
List<string> list = new List<string>();
list.Add("A");
list.Add("A");
list.Add("C");
list.Add("D");
list.Add("B");
list.Add("A");
list.Add("E");
list.Add("E");
// Get distinct elements.
var distinct = (from item in list orderby item select item).Distinct();
foreach (string value in distinct)
{
Console.WriteLine("Distinct : {0}", value);
}
}
}
Subscribe to:
Posts (Atom)