Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Monday, February 22, 2016 2:45 PM
Using this C# code (that does not work) I wish to remove the dollar character or the percent character from a string resulting in a whole number such as 50 from $50 or 50%. Here is my code...
private static string Trim(string str)
{
Regex rgx = new Regex(@"<#$>"); //everything in the parenthesis is the issue.
str = rgx.Replace(str, "");
return str.ToString();
}
Thanks for any assistance.
TC
All replies (3)
Monday, February 22, 2016 5:38 PM ✅Answered | 1 vote
If you want to perform some elements of validation and only remove one ‘$’ at the beginning or ‘%’ at the end, then:
str = Regex.Replace(str, @"^\[$\]|%$", string.Empty);
Unless you only need a simple global replacement.
Monday, February 22, 2016 3:13 PM
Why are you using RE for this? Just use String.Replace or Trim.
//If you only want to remove the leading and trailing chars
return str.Trim(new [] { '$', '%' });
//If you want to remove them anywhere in string
return str.Replace('$', default(char)).Replace('%', default(char));
Michael Taylor
http://blogs.msmvps.com/p3net
Tuesday, February 23, 2016 4:06 AM
Excellent help Viorel. Worked like a charm.
@CoolDadTx, Its helpful to assist someone with what they need instead of making such a statement. We all need to learn what we want to learn and as Viorel explained what I had to do and showed me my issue has been resolved.