c# - 使用扩展方法替换字符串

标签 c# .net

<分区>

如果我有字符串,并且我想用星号替换该字符串中的最后一个字符。

我试过了

var myString = "ABCDEFGH";
myString.ReplaceCharacter(mystring.Length - 1, 1, "*"); 

public static string ReplaceCharacter(this string str, int start, int length, string replaceWith_expression)
{
    return str.Remove(start, length).Insert(start, replaceWith_expression);
}

我尝试使用此扩展方法,但这不起作用。 为什么这不起作用?

最佳答案

方法,照原样替换字符,但你必须捕获结果

myString = myString.ReplaceCharacter(myString.Length - 1, 1, "*"); 

关于c# - 使用扩展方法替换字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30489183/

相关文章:

c# - 开始,停止,暂停和继续运行很长时间的方法

c# - 检索 JIT 输出

c# - 有没有办法使用 Microsoft.SharePoint.Client 命名空间与其他用户一起创建新的列表项

.net - 在 .NET 中优化 System.Drawing 的 PNG 输出

c# - 操作系统命令注入(inject)

c# - 如何从字符串中删除除第一次出现的字符以外的所有字符?

c# - 在 C# 中从模型自动生成好友类

c# - 使用 linq 自引用连接

c# - 多列数据转换

.net - MVCContrib 可移植区域 Swf 嵌入式资源