简体   繁体   English

如何获取字符串中单词之间的每个数字作为要替换的单独值

[英]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. 我的问题是,如何使字符串中的单词之间的每个数字作为单独的值分别替换为46,000,000(四千六百万)和70,000,000(七千万)。 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 距太阳的距离,范围从replace1到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. 而wordString将是结果字符串,其中包含数字的文字。

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(“ 46000000”,“四千六百万”);

inputStr = inputStr.Replace("70 000 000 ", "seventy million"); inputStr = inputStr.Replace(“ 700000000”,“七千万”);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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