我创建了一个方法“uniform”,用小数替换非数字字符并在间隙中关闭,但我无法运行该方法,输出也不返回结果。有人可以解释导致此输出的原因吗?
public class CalcTest
{
public String[] uniform(string[] numbers)
{
foreach(string number in numbers)
{
foreach(char character in number)
{
if(char.IsLetterOrDigit(character) == false)
{
number.Replace(character, '.');
return numbers;
}
}
if(number.Contains(" "))
{
number.Replace(" ","");
return numbers;
// This method is supposed to return the correct version of the string array in the parameters
// without the non-digits and spaces
}
}
return numbers;
}
}
class Program
{
static void Main(string[] args)
{
string [] numbers = {"1.5", "2$ 3", "12 3"};
Console.WriteLine(uniform(numbers));
// Output : And uniform does not exist in current context
// Output : System.String[]
/* These are the main outputs the compiler has*/
}
}
答案 0 :(得分:0)
请参阅代码中的注释。请注意,您所遵循的逻辑可能需要稍微改变一下才能获得您期望的输出。
提示:对于空格,char.IsLetterOrDigit(character)
为false,因此您将用空格替换空格。才有机会用空字符串替换它们。
可能有一些方法可以用更优雅的方式做你想要做的事情,或者很可能用Linq单线程做,但我建议你尝试一点一点地改进你的代码。
public String[] uniform(string[] numbers)
{
// foreach(var number in numbers) will not let you change the value
// of the number iteration variable, you need to work by array index
for(int i = 0 ; i < numbers.Length; i++)
{
foreach(char character in numbers[i])
{
if(char.IsLetterOrDigit(character) == false)
{
// when you Replace you need to assign the resulting value
// or you will just discard it
numbers[i]=numbers[i].Replace(character, '.');
//return numbers; // not so fast! You're still looping
}
}
if(numbers[i].Contains(" "))
{
// same as previous replace, you need to assign somewhere
numbers[i]=numbers[i].Replace(" ","");
// return numbers; // not so fast! You're still looping
}
}
return numbers; // you just need to return at the end of the iterations
}
答案 1 :(得分:0)
您的代码在各方面都毫无意义。请改用:
public class CalcTest
{
public void uniform(ref string[] numbers)
{
string[] ret = new string[numbers.Length];
for(int i = 0; i < numbers.Length; i++)
{
StringBuilder sb = new StringBuilder();
for(int j = 0; j < number.Length; j++)
{
char c = number[j];
if(!char.IsLetterOrDigit(c))
{
sb.Append('.');
}
else if (char.IsWhitespace(c))
{
sb.Append(',');
}
else
{
sb.Append(c);
}
}
ret[i] = sb.ToString();
}
numbers = ret;
}
}
答案 2 :(得分:0)
我不会重申我和其他人在上面的评论中提出的所有内容,但下面是代码,它将执行您所期望的内容。最大的变化是我使用System.Text.RegularExpressions.Replace方法进行更改:
public class CalcTest
{
public string[] uniform(string[] numbers)
{
Regex re = new Regex("[^0-9]");
for (int i = 0; i < numbers.Length; i++)
{
string number = numbers[i].Replace(" ", "");
number = re.Replace(number, ".", numbers[i].Length);
numbers[i] = number;
}
return numbers;
}
}
首先是替换空格,然后替换所有非数字字符。模式[^0-9]
匹配任何不是0到9的东西,第三个参数只是将其替换为字符串中的字符的次数。
以下代码显示了如何创建CalcTest
类的实例,以便您可以调用方法uniform
,然后打印出结果:
class Program
{
static void Main(string[] args)
{
string [] numbers = {"1.5", "2$ 3", "12 3"};
CalcTest cTest = new CalcTest();
string[] result = cTest.uniform(numbers);
foreach (string number in numbers)
{
Console.WriteLine(number);
}
}
}
这不是最优雅的方式,你会在记忆中结束很多字符串,但它已经过了我的睡觉时间,所以我无法提供更优雅的解决方案。
输出结果为:
1.5 2.3 123