gpt4 book ai didi

c# - String.Replace 在 VB 中工作但在 C# 中不工作

转载 作者:太空狗 更新时间:2023-10-30 00:25:38 25 4
gpt4 key购买 nike

以下 VB 代码工作正常,没有标记任何错误。

strLine = strLine.Replace(strLine.LastIndexOf(","), "")

但是相同的 C# 代码不会:

strLine = strLine.Replace(strLine.LastIndexOf(","), "");

这不会像它说的那样编译

The best overloaded method for 'string.Replace(string,string)' has some invalid arguements.

为什么这在 VB 中有效,但在 C# 中却无效?我该如何解决这个问题?

我认为它可能类似于 C# string.Replace doesn't work但这意味着该代码实际上会编译。

与其他字符串一样。替换问题:string.Replace (or other string modification) not working , 看来他们实际上会编译,而我的不会。

最佳答案

LastIndexOf返回一个整数,而不是一个字符串。 Replacestring, string 作为参数,您将其传递给 int, string,因此出现异常 The best overloaded method for 'string.Replace(string,string) ' 有一些无效的争论。

如果你只是想删除所有的 , 使用这个:

strLine = strLine.Replace(",", "");

根据您的代码,您可能只想替换 的最后一个实例, 因此,如果这是您想要的,请试试这个:

StringBuilder sb = new StringBuilder(strLine);
sb[strLine.LastIndexOf(",")] = "";
strLine = sb.ToString();

关于c# - String.Replace 在 VB 中工作但在 C# 中不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16344685/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com