【问题标题】:How to split a string based on every third character regardless of what the character is in c# [duplicate]无论c#中的字符是什么,如何根据每三个字符分割一个字符串[重复]
【发布时间】:2012-09-16 00:52:05
【问题描述】:

可能重复:
.NET String.Format() to add commas in thousands place for a number

我正在尝试将逗号添加到表示层的数字中,并且需要强制转换,然后每隔三个字符拆分一个数字,以便加入“,”。

所以如果我有这样的字符串

546546555

想要的输出:

546,546,555

其他时候,数字可能更长或更短:

254654

想要的输出:

254,654

是否可以这样拆分,然后用逗号连接?

赞!

编辑:

大家好,

非常感谢您的帮助。

要添加到这篇文章,我还找到了一种在 SQL 中执行此操作的方法:

SUBSTRING(CONVERT(varchar, CAST(NumItems AS money), 1), 0, LEN(CONVERT(varchar, CAST(NumDocs AS money), 1)) - 2) as [NumDocs]

【问题讨论】:

  • 它总是以 10 为基数吗?
  • 你要这样做吗...string.Format("{0:#,#}", 5465465555 )
  • @JamesMichaelHare 嗨詹姆斯,它永远是整数,永远不会是小数。
  • @GeneS 嗨,Gene,看起来不错,我现在试试,我是新手程序员,没想到我有那个格式选项
  • long.Parse("546546555").ToString("N0")

标签: c#


【解决方案1】:

与其手动拆分字符串,不如将其转换为数字(或保留为数字),并使用适当的格式调用ToString 方法:

示例

int value = 546546555;
string displayValue = value.ToString("#,#");

查看this MSDN page了解不同的格式值:

C - 货币格式

D - 十进制格式

E - 科学格式

F - 定点格式

G - 一般格式

N - 数字格式

P - 百分比格式

R - 往返格式

X - 十六进制格式

【讨论】:

  • 非常感谢,一切正常。
  • 你知道在 sql server 2008 中模仿这种逻辑的方法吗?
  • 这是个好问题。我没有答案,但为什么不把它官方化,看看社区能想出什么。
【解决方案2】:

您应该通过将字符串转换为整数,使用Parse 或理想情况下TryParse 并使用字符串格式来显示它:

var str = "546546555";
var formatted = String.Empty;
int value = 0;
if(int.TryParse(str,out value))
{
   formatted = value.ToString("#,#");
}

现场示例:http://rextester.com/FHO11833

【讨论】:

    【解决方案3】:

    假设您不只是尝试输出数字,这里有一个快速功能,我相信它可以满足您的需求:

    string splitter(string tosplit, int num, string splitstring)
        {
            string output = "";
            for (int i = 0; i < tosplit.Length; i += num)
                if (i + num < tosplit.Length)
                    output += tosplit.Substring(i, num) + ",";
                else
                    output += tosplit.Substring(i);
            return output;
        }
    

    这里,splitter("546546555", 3, ",") 的输出将是 546,546,555

    但这对于数字来说并不理想,因为其他答案将完美地涵盖这种情况。

    【讨论】:

      【解决方案4】:

      不是很好的代码,但它可以工作。

      public static string GetString(string val, int number)
              {
                  List<string> res = new List<string>();
                  res.Add("");
      
                  int counter = 0, i = 0;
                  while (i < val.Length)
                  {
                      while (res[counter].Length < number && i < val.Length)
                      {
                          res[counter] += val[i];
                          i++;
                      }
                      res.Add("");
                      counter++;
                  }
      
                  return string.Join(",", res.Where(r => !string.IsNullOrEmpty(r)));
              }
      

      val - 你的输入字符串 number - 要拆分的字符数,在您的情况下等于 3

      【讨论】:

        【解决方案5】:

        Gene S 和 Dan 似乎有答案,恕我直言。使用内置格式的好处是您可以编写可本地化的代码。例如,“,”是美国的数字组分隔符,但“.”是美国的数字组分隔符。在西班牙使用。

                var val = 12345678;
                CultureInfo c = CultureInfo.CurrentCulture;
                Application.CurrentCulture = new CultureInfo("EN-us");
                var s = String.Format("{0:#,#}", val);
                Application.CurrentCulture = new CultureInfo("ES-es");
                var i = String.Format("{0:#,#}", val);
                Application.CurrentCulture = c;
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2015-10-18
          • 1970-01-01
          • 1970-01-01
          • 2018-07-30
          • 1970-01-01
          • 1970-01-01
          • 2016-11-07
          • 2017-11-30
          相关资源
          最近更新 更多