C# 返回C中的数组#
我创建了一个“uniform”方法,它用小数替换非数字字符,并在空格中闭合,但我无法运行该方法,并且输出不会返回结果。有人能解释是什么导致了这种输出吗C# 返回C中的数组#,c#,C#,我创建了一个“uniform”方法,它用小数替换非数字字符,并在空格中闭合,但我无法运行该方法,并且输出不会返回结果。有人能解释是什么导致了这种输出吗 public class CalcTest { public String[] uniform(string[] numbers) { foreach(string number in numbers) { foreach(char character in numbe
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*/
}
}
请参见代码中的注释。请注意,您所遵循的逻辑可能仍需要稍作更改,以获得预期的输出 提示:
char.IsleterOrdigit(character)
对于空格为false,因此您将用替换空格。在有机会用空字符串替换它们之前
可能有一些方法可以以更优雅的方式或者很可能使用Linq one liner来完成您想要做的事情,但是我建议您尝试一点一点地改进代码
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
}
公共字符串[]统一(字符串[]数字)
{
//foreach(数值中的var number)不允许您更改该值
//对于number迭代变量,您需要按数组索引进行操作
for(int i=0;i
您的代码在许多方面都是毫无意义的。改用这个:
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;
}
}
公共类计算测试
{
公共无效统一(参考字符串[]编号)
{
string[]ret=新字符串[numbers.Length];
for(int i=0;i
我不会重复我和其他人在上面的评论中所说的一切,但下面的代码可以实现您的期望。所做的最大更改是我使用了以下方法进行更改:
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;
}
}
这不是最优雅的方式,你会在记忆中留下很多字符串,但这已经过了我的就寝时间,所以我无法给出更优雅的解决方案
输出为:
1.5
2.3
123
1.5
2.3
123
第一个问题:
string
是不可变的。调用Replace
并忽略返回值是毫无意义的。除了Jon Skeet的观察之外……首先,如果没有类的实例,就不能调用类的实例方法。您需要一个CalcTest
的实例来调用方法uniform
;其次,您看到System.String[]
的原因是ToString()
方法返回(默认情况下,除非重写)对象的类型。您将需要遍历数组并打印出值。在完成循环中的工作后,您应该返回数字
。您发布的代码中还有一个问题-一旦找到需要更改的匹配项,您就进行更改(当然不是真的),然后从方法返回,不检查数组的其余部分。最后一个注意事项-您说它应该用数字替换非数字字符-但是IsDigitOrLetter
将为数字或字母返回true,因此类似“a21”的内容将导致“…”。
1.5
2.3
123