[英]How to replace only one part of a sub string from a string containing more than one similar parts?
我可能没有将这个问题说成是我想要的。 请考虑以下情况。
场景:
我正在我的C#Win Form应用程序中实现搜索/替换功能。 此功能可以选择替换“以...开头”或“以...结尾”某个值的子字符串。 例如:
"123ABCD"
。 用"XYZ"
替换"123"
应该产生: "XYZABCD"
"ABCD123"
。 用"XYZ"
替换"123"
应该产生: "ABCDXYZ"
这两个功能都运行正常。 我的问题是当字符串包含"123ABCD123"
。 使用"XYZ"
时,两个操作都返回错误的值。
"XYZABCDXYZ"
,而不是"XYZABCD"
"XYZABCDXYZ"
而不是"ABCDXYZ"
任何人都可以告诉我如何实现这一目标吗?
谢谢 !!!
代码片段:
if (this.rbMatchFieldsStartedWith.Checked)
{
if (caseSencetive)
{
matched = currentCellValue.StartsWith(findWhat);
}
else
{
matched = currentCellValue.ToLower().StartsWith(findWhat.ToLower());
}
}
else if (this.rbMatchFieldsEndsWith.Checked)
{
if (caseSencetive)
{
matched = currentCellValue.EndsWith(findWhat);
}
else
{
matched = currentCellValue.ToLower().EndsWith(findWhat.ToLower());
}
}
if (matched)
{
if (replace)
{
if (this.rbMatchWholeField.Checked)
{
currentCell.Value = replaceWith;
}
else
{
currentCellValue = currentCellValue.Replace(findWhat, replaceWith);
currentCell.Value = currentCellValue;
}
this.QCGridView.RefreshEdit();
}
else
{
currentCell.Style.BackColor = Color.Aqua;
}
}
对于正则表达式来说,这听起来很不错。
它受.NET支持,并且还具有替换语法。
根据搜索模式实现替换方法。
更换线
currentCellValue = currentCellValue.Replace(findWhat, replaceWith);
同
if (this.rbMatchFieldsStartedWith.Checked)
{
// target string starts with findWhat, so remove findWhat and prepend replaceWith
currentCellValue = replaceWith + currentCellValue.SubString(findWhat.Length);
}
else
{
// target string end with findWhat, so remove findWhat and append replaceWith.
currentCellValue = currentCellValue.SubString(0, currentCellValue.Length - findWhat.Length) + replaceWith;
}
currentCell.Value = newValue;
我只想尝试不使用正则表达式的替换方法。
(正则表达式可能是正确的方法,但找到替代方案很有趣)
void Main()
{
string test = "123ABCD123"; // String to change
string rep = "XYZ"; // String to replace
string find = "123"; // Replacement string
bool searchStart = true; // Flag for checkbox startswith
bool searchEnd = true; // Flag for checkbox endswith
bool caseInsensitive = true; // Flag for case type replacement
string result = test;
int pos = -1;
int lastPos = -1;
if(caseInsensitive == true)
{
pos = test.IndexOf(find, StringComparison.InvariantCultureIgnoreCase);
lastPos = test.LastIndexOf(find, StringComparison.InvariantCultureIgnoreCase);
}
else
{
pos = test.IndexOf(find, StringComparison.Ordinal);
lastPos = test.LastIndexOf(find, StringComparison.Ordinal);
}
result = test;
if(pos == 0 && searchStart == true)
{
result = rep + test.Substring(find.Length);
}
if(lastPos != 0 && lastPos != pos && lastPos + find.Length == test.Length && searchEnd == true)
{
result = result.Substring(0, lastPos) + rep;
}
Console.WriteLine(result);
}
首先,我们假设:
只需阅读您的代码。 我们点击if (this.rbMatchFieldsStartedWith.Checked)
并且评估为true。 所以我们介入那个区块。 我们点击matched = currentCellValue.StartsWith(findWhat);
和matched = true
。 我们继续if (matched)
条件也评估为true
。 之后if (replace)
计算结果为true
。 最后,我们用if (this.rbMatchWholeField.Checked)
做出最后的决定,结果为false
所以我们继续使用else
块:
currentCellValue = currentCellValue.Replace(findWhat, replaceWith);
currentCell.Value = currentCellValue;
在该块中第一行替换的所有出现findWhat
与replaceWith
,123即所有出现与XYZ。 当然,这不是理想的行为。 而不是Replace
您必须使用一个函数,根据当然的输入,只替换字符串的第一个或最后一个出现。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.