There is a string that I want to translate into a number (for example, String myString ="2 014" ), but since it has a space, it throws an Exception. How can I delete a space in the line?
- oneWhat number should be? integer or fractional? - Grundy
- @Grundy integer. - Pavel Kushnerevich
- oneand the space is just the thousands separator? or can stand anywhere on the line? - Grundy
- onemyString.Replace ("", "") not? - vitidev
- one"hundredths are always separated by the thousand" ??? - Igor
5 answers
You can like this:
String myString = "2 01 4"; int count = int.Parse(myString.Replace(" ", string.Empty)); The int.Parse method has several overloads that accept the style of numbers and the format provider.
So, we have a line with group separator (thousands):
String myString = "2 014"; First of all, you need to specify the style that allows the separator groups:
int n = int.Parse(myString, NumberStyles.AllowThousands); This will work if the current culture uses exactly the space as the group separator. You can learn it, for example, like this:
var ci = CultureInfo.CurrentCulture; Console.WriteLine(ci.Name); Console.WriteLine("'" + ci.NumberFormat.NumberGroupSeparator + "'"); In Russian culture, with the name ru-RU is the default space. But if the application is running on a computer with a different culture, the separator may be different. Say, in English-language en-US this will be a comma: , .
Therefore, it is better not to rely on the default culture, but to set the format provider you need.
int n = int.Parse(myString, NumberStyles.AllowThousands, new CultureInfo("ru-RU")); in this case, the explicit creation of the ru-RU culture is used.
Since only a small part of the culture parameters is needed to parse the number, you can simply create a number format provider:
NumberFormatInfo nfi = new NumberFormatInfo(); nfi.NumberGroupSeparator = " "; int n = int.Parse(myString, NumberStyles.AllowThousands, nfi); There is clearly set the space as a separator groups. Culture as such is not used.
Finally, note that the values of the enumeration of NumberStyles can be combined. If, say, spaces can be still at the beginning and at the end of the line to be parsed, then add:
var nfi = new NumberFormatInfo(); nfi.NumberGroupSeparator = " "; var styles = NumberStyles.AllowThousands | NumberStyles.AllowLeadingWhite | NumberStyles.AllowTrailingWhite; int n = int.Parse(myString, styles, nfi); Thus, you can very flexibly customize the parsing of lines.
In the Parse method, you can pass a flag ( NumberStyles.AllowThousands ) indicating that the transmitted string uses a special character to separate the groups, depending on the established culture.
If the current culture is ru-RU , then NumberGroupSeparator is a space, so the code might look like this:
var num = int.Parse("2 134", System.Globalization.NumberStyles.AllowThousands); To make a more independent decision, the third parameter in Parse can be passed to NumberFormatInfo , in which you can set the required separator, for example:
var num = int.Parse("2 134", System.Globalization.NumberStyles.AllowThousands, new System.Globalization.NumberFormatInfo() { NumberGroupSeparator = " " }); What is interesting, when setting this flag, even despite the value of the NumberGroupSizes property specifying three digits in the group, the value in which each digit will be divided by each digit will also be correctly parsed:
int.Parse("2 1 3 4", NumberStyles.AllowThousands, new NumberFormatInfo() { NumberGroupSeparator = " ", }) == 2134 Try this
string sNumber = "23 45 5558"; string number=null; foreach (var item in sNumber.Split(' ')) { number += item; } int convertNumber = int.Parse(number); - Why collect by hand ??? If you walk then it is already full:
string.Join("", sNumber .Split());- nick_n_a - @nick_n_a wrote immediately, did not think so, but thanks so much;) - Vardan Vardanyan
It is possible so:
int count = Convert.ToInt32(myString.Replace(" ", ""));