简体   繁体   中英

How to get each number between words in string as separate value to replace

First I've to say that's I'm not asking, how to convert integer value to the string phrase.

My question is how to get each number exist between words in string as separate value to replace 46 000 000 with forty-six million and 70 000 000 with seventy million. To make it easier and not confuse for example if string value already exists:

string val1 = "replace1";
string val2 = "replace2";

and input string:

  string inputStr = "distance from the Sun ranging from 46 000 000 to 70 000 000 km";

to get this result:

distance from the Sun ranging from replace1 to replace2 km

I'm not asking how to replace value to other value, my question is how to extract exist numbers from string as separate values to replace it after.

string foundNum1 = "46 000 000";
string foundNum2 = "70 000 000";

Only then I can use replace method:

string f1 = inputStr.Replace(foundNum1, val1);
string outStr = f1.Replace(foundNum2, val2);

Here is a complete answer for any number you need to convert:

 public static string NumberToWords(int number)
    {
        if (number == 0)
            return "zero";

        if (number < 0)
            return "minus " + NumberToWords(Math.Abs(number));

        string words = "";

        if ((number / 1000000) > 0)
        {
            words += NumberToWords(number / 1000000) + " million ";
            number %= 1000000;
        }

        if ((number / 1000) > 0)
        {
            words += NumberToWords(number / 1000) + " thousand ";
            number %= 1000;
        }

        if ((number / 100) > 0)
        {
            words += NumberToWords(number / 100) + " hundred ";
            number %= 100;
        }

        if (number > 0)
        {
            if (words != "")
                words += "and ";

            var unitsMap = new[] { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen" };
            var tensMap = new[] { "zero", "ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety" };

            if (number < 20)
                words += unitsMap[number];
            else
            {
                words += tensMap[number / 10];
                if ((number % 10) > 0)
                    words += "-" + unitsMap[number % 10];
            }
        }

        return words;
    }


    public string ConvertStringNumbersToWrittenForm(string inputStr)
    {
        var regex = new Regex(@"[\d][\d ]+[\d]");

        var matches = regex.Matches(inputStr);
        string newString = inputStr;
        foreach (var match in matches)
        {
            string matchStr = match.ToString();
            //eliminate spaces
            matchStr = matchStr.Replace(" ", string.Empty);
            newString = newString.Replace(match.ToString(), NumberToWords(Convert.ToInt32(matchStr)));
        }

        return newString;
    }

To use it, you call:

string wordString = ConvertStringNumbersToWrittenForm("distance from the Sun ranging from 46 000 000 to 70 000 000 km");

and wordString will be the resulting string, with written words for the numbers.

To do this dynamically, you could use a regex like follows:

string val1 = "replace1";
string val2 = "replace2";

string inputStr = "distance from the Sun ranging from 46 000 000 to 70 000 000 km";

var regex = new Regex(@"[\d][\d ]+[\d]");

var matches = regex.Matches(inputStr);

inputStr.Replace(matches[0], val1);
inputStr.Replace(matches[1], val2);

Just note the following:

  • if there are not two matches, this won't wokr
  • if the matches are the same, this will always replace both
  • there are probably other edge cases

This will get you back any group of numbers.

string inputStr = "distance from the Sun ranging from 46 000 000 to 70 000 000 km";
Regex pattern = new Regex(@"([0-9]{1,3}(?:\s[0-9]{3})*)");

foreach (Match match in pattern.Matches(inputStr))
{
    Console.WriteLine(match.Groups[1]);
}

This displays each match. Now you would just need to do your replace the way you want. Not sure what you are planning there; that's why I didn't put anything in place.

您可以执行以下操作:

string res = inputStr.Replace("46 000 000", val1).Replace("70 000 000", val2);

I'm pretty sure you do it like this:

inputStr = inputStr.Replace("46 000 000", "forty six million");

inputStr = inputStr.Replace("70 000 000 ", "seventy million");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM