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

検索モードに応じた置換方法を実装します。

ラインを交換する

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)し、どれが 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;

このブロックの最初の行は、出現するすべての を に置き換えますfindWhatreplaceWithつまり、出現するすべての 123 を XYZ に置き換えます。もちろん、これは望ましい動作ではありません。代わりにReplace、もちろん入力に従って文字列の最初または最後の出現のみを置き換える関数を使用する必要があります。

于 2013-04-12T07:34:51.273 に答える