C# 有限变量方法中的字符串替换
嗨,我想纠正一个输入了3个变量的练习中的错误。我正在努力寻找一种方法来做这件事。我不能使用“replace”,因为它需要两个字符串变量。我试过在子字符串上乱搞,但还没弄明白C# 有限变量方法中的字符串替换,c#,C#,嗨,我想纠正一个输入了3个变量的练习中的错误。我正在努力寻找一种方法来做这件事。我不能使用“replace”,因为它需要两个字符串变量。我试过在子字符串上乱搞,但还没弄明白 using System.Text; using System.Threading.Tasks; namespace Week_9_Ex1 { class Program { static void Main(string[] args) {
using System.Text;
using System.Threading.Tasks;
namespace Week_9_Ex1
{
class Program
{
static void Main(string[] args)
{
Program fix = new Program();
Console.WriteLine(fix.FixTypo("The Wilking Dead", 5, "a"));
}
public string FixTypo(string needCorrect, int index, string replacement)
{
needCorrect.Substring(index, 1);
return needCorrect;
}
}
}
第一期: 字符串是。为了在任何地方使用该值,必须在使用
Substring
后将该字符串指定给某个对象
在您的问题中,FixTypo
会得到一个子字符串needCorrect
,但会简单地丢弃它,因为它没有分配给任何对象。相反,您可以将其分配给自身:
needCorrect = needCorrect.Substring(index, 1)
…或return
直接返回(对返回本身进行赋值)
第二期:要回答问题的主要部分,您可以使用String.Concat
或+
运算符轻松地将needCorrect
的多个子字符串连接在一起
例如,这是因为您直接将其分配给退货:
public string FixTypo(string needCorrect, int index, string replacement)
{
return needCorrect.Substring(0, index) + replacement + needCorrect.Substring(index + 1);
}
在本例中,+
运算符将三个字符串连接在一起。第一个和第三个字符串是原始字符串的子字符串。第一个子串是从0
到index
(要进行替换的点(W
)。第三个子串是从index+1
到原始字符串的末尾(lking Dead
)
中间的字符串只是要用于替换(
a
)的值,在索引中您可以使用substring进行替换:
public static string FixTypo(string needCorrect, int index, string replacement)
{
// Do some argument checks to avoid exceptions
if (needCorrect == null ||
needCorrect.Length < index + 1 ||
index < 0 ||
replacement == null)
{
// Note, you may want to throw an ArgumentNullException or
// ArgumentOutOfRange exception instead...
return needCorrect;
}
return needCorrect.Substring(0, index) + replacement[0] +
needCorrect.Substring(index + 1);
}
publicstaticstringfixtypo(stringneedcorrect、int-index、stringreplacement)
{
//进行一些参数检查以避免异常
if(needCorrect==null||
需要正确。长度<索引+1 | |
指数<0 | |
替换==空)
{
//注意,您可能希望抛出ArgumentNullException或
//改为ArgumentOutOfRange异常。。。
返回正确;
}
返回正确。子字符串(0,索引)+替换[0]+
needCorrect.子字符串(索引+1);
}
假设grovesNL的答案是错误的,并且您实际上想要替换该值:
using System.Text;
using System.Threading.Tasks;
namespace Week_9_Ex1
{
class Program
{
static void Main(string[] args)
{
Program fix = new Program();
Console.WriteLine(fix.FixTypo("The Wilking Dead", 5, "a"));
}
public string FixTypo(string needCorrect, int index, string replacement)
{
return string.Concat(needCorrect.Substring(0, index), replacement, needCorrect.Substring(index+1));
}
}
}
字符串是不可变的,您需要从string.Substring
:needCorrect=needCorrect.Substring(index,1);
如何将needCorrect值放入变量中减去不需要的字符串?是否始终只替换单个字符?返回string.Concat(needCorrect.Substring(0,index),replacement,needCorrect.Substring(index+1))是的,仅替换单个字符,因为我不能仅用一个整数变量指定长度我建议在这里使用string.Concat,而不是string+string+string。至少在较旧的.NET版本中(我没有检查最近的版本)最好这样做,string+string通常会生成.NET IL代码,该代码本质上创建了一个字符串数组,然后将两个元素连接起来。好吧,忽略这一点,情况似乎不再如此。
using System.Text;
using System.Threading.Tasks;
namespace Week_9_Ex1
{
class Program
{
static void Main(string[] args)
{
Program fix = new Program();
Console.WriteLine(fix.FixTypo("The Wilking Dead", 5, "a"));
}
public string FixTypo(string needCorrect, int index, string replacement)
{
return string.Concat(needCorrect.Substring(0, index), replacement, needCorrect.Substring(index+1));
}
}
}