I might have not stated the question as what I would like to. Please consider below scenario. 

**Scenario:**  

I am implementing a Search/Replace functionality in my C# Win Form application. This feature will have the option to replace a substring that "starts with" or "ends with" a certain value. For example:

* A string contains `"123ABCD"`. Replacing `"123"` with `"XYZ"` should produce: `"XYZABCD"`
* A string contains `"ABCD123"`. Replacing `"123"` with `"XYZ"` should produce: `"ABCDXYZ"`

Both of these features are working fine. My problem is when the string contains `"123ABCD123"`. Both operations return the wrong value when using `"XYZ"`.

* "starts with" produces `"XYZABCDXYZ"`, instead of `"XYZABCD"`
* "ends with" produces `"XYZABCDXYZ"` instead of `"ABCDXYZ"`

Can anyone give me an idea how to achieve that? 

Thanks !!! 

**Code Snippet:** 

```
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;
    }
}

```