Remove Characters from C# String

How do you remove all the alphabetic characters from a string?

This should work:

// add directive at the top 
using System.Text.RegularExpressions;

string numberOnly = Regex.Replace(s, "[^0-9.]", "")

Remove specific characters from string C#

By the power of Regex!

var x = Regex.Replace(tags, @"(\[|""|\])", "");

List string smart way to remove characters from items

My suggestion would be to use LINQ. See an example below:

List<string> stringlist = new List<string> { "RGBI1red", "green", "RGBI1pink", "purple" };
stringlist = stringlist.Select(s => s.Replace("RGBI1", string.Empty)).ToList();

Using the list that you've made, this select query will replace all RGBI1 with nothing. This then is converted into a list by the use of .ToList().

Take a look at this question for more information.

Remove '\' char from string c#

You could use:

line.Replace(@"\", "");

or

line.Replace(@"\", string.Empty);

Remove characters between in c#

A simple solution would be:

strValue = strValue.Substring(0, strValue.LastIndexOf("rgm")) + strValue.Substring(strValue.LastIndexOf(";1") + 2);

EDIT:

According to your edit, it seems you want all occurrences replaced. Also, your expected result has the "." removed as well. To replace all occurrences you can adapt from @Damith's answer:

strValue = Regex.Replace(strValue, "rgm.*?;1\\.", "");

Remove a character in C#

It is easier to handle it as a string instead of trying to parse and then format it as number:

return Regex.Replace(phone, @"(\d{3})-(\d{3})-(\d{4})", "($1) $2 - $3")

Removing multiple characters from a string in c# using MyString.Remove(x,y)

Given

public static IEnumerable<string> GetStuff(string input)
{
Console.WriteLine(input.Length);
if (input.Length == 46)
yield return input.Substring(4, 40);
else
for (var i = 0; i < input.Length; i += 44)
yield return input.Substring(i + 2, 40);
}

Usage

var input = "xxxx1234567890123456789012345678901234567890xx";
var input2 = "xx1234567890123456789012345678901234567890xxxx1234567890123456789012345678901234567890xxxx1234567890123456789012345678901234567890xxxx1234567890123456789012345678901234567890xx";
Console.WriteLine(string.Join("\r\n", GetStuff(input)));
Console.WriteLine();
Console.WriteLine(string.Join("\r\n", GetStuff(input2)));

Output

46
1234567890123456789012345678901234567890

176
1234567890123456789012345678901234567890
1234567890123456789012345678901234567890
1234567890123456789012345678901234567890
1234567890123456789012345678901234567890

Full Demo Here

C# String Remove characters between two characters

void Main()
{
string pattern = @"\n{0,1}//RemoveFromhere(.|\n)*?//RemoveTohere\n{0,1}";
var result = Regex.Replace(sample, pattern, "");
Console.WriteLine(result);
}

static string sample = @"Here
//RemoveFromhere
<div>
<p>my name is blawal i want to remove this div </p>
</div>
//RemoveTohere
Keep this.
//RemoveFromhere
<div>
<p>my name is blawal i want to remove this div </p>
</div>
//RemoveTohere
Keep this too.
";


Related Topics



Leave a reply



Submit