首页 > 解决方案 > 用 C# 中的单词替换字符串中的 0 - 10 个数字

问题描述

我面临的问题是我必须将输入中的 0 - 10 替换为它们各自的单词,如“2” - 两个,这将是一个字符串。示例:当输入显示“11 个苹果和 2 个芒果”时,我得到的输出是“一个苹果和两个芒果”。预期输出:“11 个苹果和两个芒果”。这是我的一次尝试,但失败了。

 string str = Console.ReadLine();
                string[] strArr = str.split(" ");
                Regex rgx = new Regex("^[0-9]{2}");
                var val = rgx.isMatch(strAtr[i]);
                if(!val){
                        StringBuilder sb = new StringBuilder(str);
                        sb.Replace("0", "zero");
                        sb.Replace("1", "one");
                        sb.Replace("2", "two");
                        sb.Replace("3", "three");
                        sb.Replace("4", "four");
                        sb.Replace("5", "five");
                        sb.Replace("6", "six");
                        sb.Replace("7", "seven");
                        sb.Replace("8", "eight");
                        sb.Replace("9", "nine");
                        sb.Replace("10", "ten");
                      }
                       Console.WriteLine(sb.ToString());

标签: c#stringreplace

解决方案


我将在下面留下我的原始答案,这是一个很好的答案,看起来你在问这个问题,直到你在评论中澄清了一些事情。

我相信这回答了你的问题:

 const string mangoesPattern = @"\d+";
 var mangoesRegex = new Regex(mangoesPattern);
 const string mangoesSource = "I have 5 apples, 11 mangoes, 675 oranges, 3 grapes and 27 coconuts";

 var mangoesDictionary = new Dictionary<int, string>
 {
     {0, "zero"},
     {1, "one"},
     {2, "two"},
     {3, "three"},
     {4, "four"},
     {5, "five"},
     {6, "six"},
     {7, "seven"},
     {8, "eight"},
     {9, "nine"},
     {10, "ten"},
 };

 var mangoesMatches = mangoesRegex.Matches(mangoesSource);
 var buffer = new StringBuilder();
 var lastProcessedIndex = 0;
 var originalStringLength = mangoesSource.Length;

 foreach (var match in mangoesMatches.Cast<Match>())
 {
     var numGroup = match.Groups[0];
     var num = int.Parse(numGroup.ToString());       //it's necessarily a number, so it should work
     if (mangoesDictionary.ContainsKey(num))
     {
         buffer.Append(mangoesSource.Substring(lastProcessedIndex, numGroup.Index - lastProcessedIndex));
         buffer.Append(mangoesDictionary[num]);
         lastProcessedIndex = numGroup.Index + numGroup.Length;
     }
 }

 //pick up the rest of the string
 buffer.Append(mangoesSource.Substring(lastProcessedIndex, originalStringLength - lastProcessedIndex));

它在源字符串中查找任何整数子字符串,然后遍历匹配项和组,检查整数值和要翻译的数字之间是否存在翻译。如果有,它会进行替换。

翻译后,原文出处:

I have 5 apples, 11 mangoes, 675 oranges, 3 grapes and 27 coconuts

结束为:

I have five apples, 11 mangoes, 675 oranges, three grapes and 27 coconuts

这是我的原始答案,当我认为您的问题是关于“如何将数字转换为文本”时。

顺便说一句,你真的需要充实你的问题。阅读所有评论并解决您的问题。使用我的苹果和芒果串作为你想要的行为的例子


这看起来像是一项家庭作业。我不做其他人的功课,但我会给出一大堆提示作为答案。

首先考虑问题空间:

  • 数字 0-19 很特殊,确实需要按照您在显示的代码中的方式进行处理(作为一组表示数字的字符串)
  • 从 20 开始一直到 99,您可以取第一个数字,给它一个名字(如“三十”)并取第一条规则中的一个数字(这样 43 就变成了“四十”、“三”)
  • 当你达到 100 时,你取第一个数字,应用第一条规则,添加字符串“百”,然后添加代表前两条规则中最后两位数字的数字(这样 823 变为“八”,“百”、“二十”、“三”)。
  • 在 1000 到 999,999 之间,您将千位视为一到三位数字,在其后加上“千”,然后是后面三位数字的名称(个/十/百部分)。结果 236,117 变为“二”、“百”、“三十”、“六”、“千”、“一”、“百”、“十七”
  • 百万、十亿等都遵循与千相同的规则,在数字前面添加更多信息。

这应该足以让您编写一些代码。

如果您考虑一下,则不需要那么多字符串。20 个 0-19,然后 8 个 20-90。然后是“百”、“千”、“百万”等。


推荐阅读