Question

I have formatted string, for example 123 456 000$ and I know format of this string {0:### ### ##0}$. I'd like to get int value = 123456000; from this string in C# with using known format.

How I can do that?

Was it helpful?

Solution

mytext = System.Text.RegularExpressions.Regex.Replace(mytext.Name, "[^.0-9]", ""); 

or something approximately like that get's rid of the pesky non-numbers

and then delete the first few characters 1,2,3... and last few length, length -1, length -3...

unless I'm missing something?

Oh yeah, and Convert.Toint32(mytext)

OTHER TIPS

int.Parse("123 456 000$", NumberStyles.AllowCurrencySymbol |
                          NumberStyles.Number);

http://msdn.microsoft.com/en-us/library/c09yxbyt.aspx

I think you will have to construct a similar NumberFormatInfo to parse the string value.$ for the currency symbol used in the text and in your case the thousand group seperator is space instead of , so a custom number format info should help you parse it.

string val = "123 456 000$";
NumberFormatInfo numinf = new NumberFormatInfo();
numinf.CurrencySymbol = "$";
numinf.CurrencyGroupSeparator = " ";
numinf.NumberGroupSeparator = " "; // you have space instead of comma as the seperator
int requiredval = int.Parse(val,NumberStyles.AllowCurrencySymbol | NumberStyles.AllowThousands,  numinf);

This should help you get the value

I would make it easy way:

String formattedString = "12345500$";
            formattedString.Remove(formattedString.Length - 1);
            int value = int.Parse(formattedString);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top