0

我可能没有按照我的意愿提出这个问题。请考虑以下情况。

场景:
我正在我的 C# Win Form 应用程序中实现搜索/替换功能。此功能可以选择替换“以”或“以”某个值结尾的子字符串。例如:

  • 一个字符串包含"123ABCD". 替换"123""XYZ"应该产生:"XYZABCD"
  • 一个字符串包含"ABCD123". 替换"123""XYZ"应该产生:"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;
    }
}
4

4 回答 4

1

这对于正则表达式来说听起来不错。

它受 .NET 支持,并且还具有替换语法。

于 2013-04-12T07:10:40.717 回答
1

实现依赖于搜索模式的替换方法。

换行

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;
于 2013-04-12T07:22:05.433 回答
0

我只想尝试不使用正则表达式的替换方法。
(正则表达式可能是正确的方法,但找到替代方法很有趣)

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);
}
于 2013-04-12T07:29:44.447 回答
0

首先,让我们跟踪您的场景,假设:

  • 要处理的字符串是 123ABCD123
  • 开头是检查。
  • 目的是将“123”替换为“XYZ”

只需阅读您的代码。我们击中if (this.rbMatchFieldsStartedWith.Checked)并且评估为真。所以我们踏入了那个街区。我们打了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;

此块中的第一行替换所有出现的findWhatwith replaceWith,即所有出现的 123 替换为 XYZ。当然,这不是期望的行为。当然,Replace您必须使用一个函数来替换字符串的第一次或最后一次出现,而不是根据输入。

于 2013-04-12T07:34:51.273 回答