augustine
augustine

Reputation: 1022

c#:String to integer conversion?

I have the string in format of Rs.2, 50,000. Now I want to read the string value as 250000 and insert into database (let assume that Rs 2,50,000 coming from third party like web services, wcf services, or from other application ).Is There any direct solution for this without using String.Replace() can u write the code to remove the commas.

Upvotes: 1

Views: 439

Answers (5)

Barry Kaye
Barry Kaye

Reputation: 7759

Something a little different:

        string rs = "Rs.2, 50,000";
        string s = String.Join("", Regex.Split(rs, @"[^\d]+"));

Upvotes: 1

FIre Panda
FIre Panda

Reputation: 6637

Try

string str = "25,000,000";
str = string.Join(string.Empty, str.Split(','));    
int i = Convert.ToInt32(str);

Upvotes: 3

V4Vendetta
V4Vendetta

Reputation: 38230

Something like this works for me

string numb="2,50,000";
int.TryParse(numb, NumberStyles.AllowThousands, CultureInfo.InvariantCulture.NumberFormat,out actualValue);

Upvotes: 8

Marco
Marco

Reputation: 57593

Try this:

string rs = "Rs.2, 50,000";
string dst="";
foreach (char c in rs)
     if (char.IsDigit(c)) dst += c;

rs is source string, wihle in dst you have string you need.

Upvotes: 1

Tony The Lion
Tony The Lion

Reputation: 63310

Something like this: (I haven't tested this at all)

string str = "RS.2,50,000";
str = str.substring(3, str.length);

string arr[] = str.split(",");

string newstr;
foreach (string s in arr)
{
    newstr += s;
}

Upvotes: 3

Related Questions