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
Casting Interfaces for Deserialization in JSON.Net
Select N Random Elements from a List<T> in C#
Accessing a Variable from Another Script C#
Binding a Button's Visibility to a Bool Value in Viewmodel
Easiest Way to Create a Cascade Dropdown in ASP.NET MVC 3 with C#
Does Using "New" on a Struct Allocate It on the Heap or Stack
ASP.NET MVC Binding to a Dictionary
C# Test If User Has Write Access to a Folder
How to Check for Nulls in an '==' Operator Overload Without Infinite Recursion
Web App Blocked While Processing Another Web App on Sharing Same Session
Inner Join of Datatables in C#
Associating Enums with Strings in C#
How to Close a Login Form and Show the Main Form Without My Application Closing