-1

I have a string like this

12,3 m and i need 2 sub strings one decimal value and one unit like 12,3 and m

12.3 will return 12.3 and m

123,4 c will return 123,4 and c

The decimal separators can be . or ,

So how can i get it in C# without iterating through every characters like below

char c;

for (int i = 0; i < Word.Length; i++)
{
  c = Word[i];

  if (Char.IsDigit(c))
    string1 += c;
  else
    string2 += c;
}

string input is not really needed to be formatted like this it can be like A12,3 m or ABC3.45 or 4.5 DEF etc. So string Split is not stable always

Sebastian
  • 4,625
  • 17
  • 76
  • 145

2 Answers2

4

Looks like you are trying to split based on the whitespace character:

input = "12.3 c";
string[] stringArray = string.Split(input, ' ');

You can then do a float.Parse operation on the first element of the array. The decimal separator used by float.Parse would depend on your culture and if the wrong one is chosen you could get a FormatException.

You can also choose the decimal separator programatically through the below:

culture.NumberFormat.NumberDecimalSeparator = "."; // or ","
user1666620
  • 4,800
  • 18
  • 27
  • string input is not really needed to be formatted like this it can be like A12,3 m or ABC3.45 or 4.5 DEF etc. So string Split is not stable always – Sebastian Aug 21 '15 at 10:03
  • 3
    @JibinMathew then put that information in your question, because it changes entirely the question and what the solutions are. Do you only want the numbers? – user1666620 Aug 21 '15 at 10:03
  • why the downvote? it answered the original question. – user1666620 Aug 21 '15 at 10:07
  • @JibinMathew you should make an EDIT to your original question more clear, e.g. EDIT: blah blah – Paul Zahra Aug 21 '15 at 10:16
0

Checking your provided examples { "12,3 m", "A12,3 m", "ABC3.45", "4.5 DEF"} it seems that the string position can not only change but there can be 2 strings and one decimal in your inputstrings.

This solution will show you how to extract these data with only one regex and no manual string split. I will incorporate the CultureInfo from user1666620:

string[] inputStrings = new string[] { "12,3 m", "A12,3 m", "ABC3.45", "4.5 DEF"};
Regex splitterRx = new Regex("([a-zA-Z]*)\\s*([\\d\\.,]+)\\s*([a-zA-Z]*)");
List<Tuple<string, decimal, string>> results = new List<Tuple<string, decimal, string>>();
foreach (var str in inputStrings)
{ 
    var splitterM = splitterRx.Match(str);
    if (splitterM.Success)
    {
        results.Add(new Tuple<string, decimal, string>(
            splitterM.Groups[1].Value,
            decimal.Parse(
                splitterM.Groups[2].Value.Replace(".", System.Globalization.NumberFormatInfo.CurrentInfo.NumberDecimalSeparator).Replace(
                    ",", System.Globalization.NumberFormatInfo.CurrentInfo.NumberDecimalSeparator)
            ),
            splitterM.Groups[3].Value
        ));
    }
}

This will find all possible combinations of a present/not present string in pre/post position, so be sure to check the individual strings or apply any combining logik unto them.

Also it doesn't only check for the presence of a single space between the decimal and the strings but for the presence of any number of whitespaces. If you want to limit it to definately only 0 or 1 space instead replace the Regex with this:

Regex splitterRx = new Regex("([a-zA-Z]*)[ ]{0,1}([\\d\\.,]+)[ ]{0,1}([a-zA-Z]*)");
Sors
  • 494
  • 1
  • 4
  • 12