如何在 foreach 循环中为 char 变量赋值? C#
How can i assign a value to a char variable in a foreach loop? C#
我想做的是这样的:
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);
}
}
我想要的是,如果这个字符是一个逗号,就把它变成一个点。但是我收到以下错误
it is not possible to assign value to 'ch' because it is a foreach interaction variable
为什么不在两个 if 分支中附加?
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);
}
}
只要引入一个局部变量
StringBuilder sb = new StringBuilder();
foreach(char ch in valor)
{
var tmp = ch == ',' ? '.' : ch;
if (char.IsDigit(tmp) || tmp == '.')
{
sb.Append(tmp);
}
}
您也可以使用静态char.IsDigit()
方法来检查一个字符是否为数字
你也可以只用 linq:
var sb = new StringBuilder();
sb = valor
.Select(c => c == ',' ? '.' : c)
.Where(c => char.IsDigit(c) || c == '.')
.Aggregate(sb, (sb, c) => sb.Append(c));
简答:不可能。
使用不可变的 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);
}
}
我删除了 else
因为它似乎是一个错误...
但在这种情况下,您可能更喜欢直接使用 for
来避免这个无用的中间变量,从而优化速度和内存:
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);
}
}
接下来,对于所提供的代码,我们可以这样重构,现在以正确的方式使用 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);
}
}
另外,在提供代码的情况下,我们可以改进为:
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);
}
}
并更好地优化:
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);
}
}
所以我们可以使用 foreach
来编写,但在更简洁的同时优化程度较低:
foreach (char ch in valor )
{
if ( ch == ',' )
{
sb.Append('.');
}
else
if ( ch >= '0' && ch <= '9' )
{
sb.Append(ch);
}
}
你可以试试这个:
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);
}
}
我想做的是这样的:
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);
}
}
我想要的是,如果这个字符是一个逗号,就把它变成一个点。但是我收到以下错误
it is not possible to assign value to 'ch' because it is a foreach interaction variable
为什么不在两个 if 分支中附加?
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);
}
}
只要引入一个局部变量
StringBuilder sb = new StringBuilder();
foreach(char ch in valor)
{
var tmp = ch == ',' ? '.' : ch;
if (char.IsDigit(tmp) || tmp == '.')
{
sb.Append(tmp);
}
}
您也可以使用静态char.IsDigit()
方法来检查一个字符是否为数字
你也可以只用 linq:
var sb = new StringBuilder();
sb = valor
.Select(c => c == ',' ? '.' : c)
.Where(c => char.IsDigit(c) || c == '.')
.Aggregate(sb, (sb, c) => sb.Append(c));
简答:不可能。
使用不可变的 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);
}
}
我删除了 else
因为它似乎是一个错误...
但在这种情况下,您可能更喜欢直接使用 for
来避免这个无用的中间变量,从而优化速度和内存:
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);
}
}
接下来,对于所提供的代码,我们可以这样重构,现在以正确的方式使用 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);
}
}
另外,在提供代码的情况下,我们可以改进为:
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);
}
}
并更好地优化:
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);
}
}
所以我们可以使用 foreach
来编写,但在更简洁的同时优化程度较低:
foreach (char ch in valor )
{
if ( ch == ',' )
{
sb.Append('.');
}
else
if ( ch >= '0' && ch <= '9' )
{
sb.Append(ch);
}
}
你可以试试这个:
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);
}
}