简体   繁体   English

如何在 foreach 循环中为 char 变量赋值? C#

[英]How can i assign a value to a char variable in a foreach loop? C#

I am trying to do is something like this:我想做的是这样的:

StringBuilder sb = new StringBuilder();

foreach(char ch in valor)
{
    if (ch == ',')
        ch = '.';
    else if (ch == '0' || ch == '1' || ch == '2' || ch == '3' || ch == '4' || ch == '5' || ch == '6' || ch == '7' || ch == '8' || ch == '9' || ch == ',')
    {
        sb.Append(ch);
    }
}

What I want is, if the character is a comma, to make it a dot.我想要的是,如果字符是逗号,则将其设为点。 But i get the following error但我收到以下错误

it is not possible to assign value to 'ch' because it is a foreach interaction variable无法为“ch”赋值,因为它是一个 foreach 交互变量

Why not append in both if branches?为什么不在两个 if 分支中使用 append?

foreach(char ch in valor)
{
    if (ch == ',')
    {
        sb.Append('.');
    }
    else if (ch == '0' || ch == '1' || ch == '2' || ch == '3' || ch == '4' || ch == '5' || ch == '6' || ch == '7' || ch == '8' || ch == '9')
    {
        sb.Append(ch);
    }
}

Just introduce a local variable只需引入一个局部变量

StringBuilder sb = new StringBuilder();

foreach(char ch in valor)
{
    var tmp = ch == ',' ? '.' : ch;

    if (char.IsDigit(tmp) || tmp == '.')
    {
        sb.Append(tmp);
    }
}

You can also use the static char.IsDigit() method to check whether a char is a digit or not您还可以使用 static char.IsDigit()方法来检查 char 是否为数字

You could also do it only with linq:你也可以只用 linq 来做:

var sb = new StringBuilder();
sb = valor
    .Select(c => c == ',' ? '.' : c)
    .Where(c => char.IsDigit(c) || c == '.')
    .Aggregate(sb, (sb, c) => sb.Append(c));

Short answer : it is impossible .简短的回答这是不可能的

Using an immutable foreach indexed item you need to use an intermediate variable like that:使用不可变的foreach索引项,您需要使用这样的中间变量:

foreach ( char ch in valor )
{
  char c = ch;
  if ( c == ',' )
  {
    c = '.';
  }
  if ( c == '0' || c == '1' || c == '2' || c == '3' || c == '4'
    || c == '5' || c == '6' || c == '7' || c == '8' || c == '9'
    || c == '.' )
  {
    sb.Append(c);
  }
}

I removed the else because it seems to be a mistake...我删除了else ,因为它似乎是一个错误......

But in this case you may prefer directly use a for to avoid this useless intermediate variable and so to optimize speed and memory:但在这种情况下,您可能更喜欢直接使用for来避免这个无用的中间变量,从而优化速度和 memory:

for (int index = 0; index < valor.Length; index++)
{
  char c = valor[index];
  if ( c == ',' )
  {
    c = '.';
  }
  if ( c == '0' || c == '1' || c == '2' || c == '3' || c == '4'
    || c == '5' || c == '6' || c == '7' || c == '8' || c == '9'
    || c == '.' )
  {
    sb.Append(c);
  }
}

Next, in the case of the code provided, we can refactor like that, using the else in the right way now:接下来,对于提供的代码,我们可以像这样重构,现在以正确的方式使用else

for (int index = 0; index < valor.Length; index++)
{
  char c = valor[index];
  if ( c == ',' )
  {
    sb.Append('.');
  }
  else
  if ( c == '0' || c == '1' || c == '2' || c == '3' || c == '4'
    || c == '5' || c == '6' || c == '7' || c == '8' || c == '9' )
  {
    sb.Append(c);
  }
}

Also, in the case of the code provided, we can improve to this:此外,在提供代码的情况下,我们可以对此进行改进:

for ( int index = 0; index < valor.Length; index++ )
{
  char c = valor[index];
  if ( c == ',' )
  {
    sb.Append('.');
  }
  else
  if ( char.IsDigit(c) )     // or IsNumber
  {
    sb.Append(c);
  }
}

And to optimize better:为了更好地优化:

for ( int index = 0; index < valor.Length; index++ )
{
  char c = valor[index];
  if ( c == ',' )
  {
    sb.Append('.');
  }
  else
  if ( c >= '0' && c <= '9' )
  {
    sb.Append(c);
  }
}

So using a foreach we can write, but a little less optimized while being more clean:因此,我们可以使用foreach编写,但优化程度较低,但更干净:

foreach (char ch in valor )
{
  if ( ch == ',' )
  {
    sb.Append('.');
  }
  else
  if ( ch >= '0' && ch <= '9' )
  {
    sb.Append(ch);
  }
}

Char.IsDigit Method Char.IsDigit 方法

Char.IsNumber Method Char.IsNumber 方法

foreach, in (C# reference) foreach,在(C# 参考)

for (C# reference) 对于(C# 参考)

You can try this:你可以试试这个:


StringBuilder sb = new StringBuilder();
var valor = "google,com1234";

if (valor.Contains(','))
{
   valor = valor.Replace(",", ".");
}
foreach (char ch in valor)
{
   if (ch == '0' || ch == '1' || ch == '2' || ch == '3' || ch == '4' || ch == '5' || ch == '6' || ch == '7' || ch == '8' || ch == '9' || ch == '.')
    {
        sb.Append(ch);
    }
}

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

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