Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple way to trim Dollar Sign if present in C#

Tags:

c#

I have a DataRow and I am getting one of the elements which is a Amount with a dollar sign. I am calling a toString on it. Is there another method I can call on it to remove the dollar sign if present.

So something like:

dr.ToString.Substring(1, dr.ToString.Length);

But more conditionally in case the dollar sign ever made an appearance again.

I am trying to do this with explicitly defining another string.

like image 450
Brian G Avatar asked Nov 27 '22 00:11

Brian G


2 Answers

Convert.ToString(dr(columnName)).Replace("$", String.Empty)

-- If you are working with a data table, then you have to unbox the value (by default its Object) to a string, so you are already creating a string, and then another with the replacement. There is really no other way to get around it, but you will only see performance differences when dealing with tens of thousands of operations.

like image 85
StingyJack Avatar answered Dec 18 '22 07:12

StingyJack


You could also use

string trimmed = (dr as string).Trim('$');

or

string trimmed = (dr as string).TrimStart('$');
like image 42
hangy Avatar answered Dec 18 '22 05:12

hangy