Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to replace only one part of a sub string from a string containing more than one similar parts?
    text
    copied!<p>I might have not stated the question as what I would like to. Please consider below scenario. </p> <p><strong>Scenario:</strong><br> 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:</p> <ul> <li>A string contains <code>"123ABCD"</code>. Replacing <code>"123"</code> with <code>"XYZ"</code> should produce: <code>"XYZABCD"</code> </li> <li>A string contains <code>"ABCD123"</code>. Replacing <code>"123"</code> with <code>"XYZ"</code> should produce: <code>"ABCDXYZ"</code> </li> </ul> <p>Both of these features are working fine. My problem is when the string contains <code>"123ABCD123"</code>. Both operations return the wrong value when using <code>"XYZ"</code>.</p> <ul> <li>"starts with" produces <code>"XYZABCDXYZ"</code>, instead of <code>"XYZABCD"</code></li> <li>"ends with" produces <code>"XYZABCDXYZ"</code> instead of <code>"ABCDXYZ"</code></li> </ul> <p>Can anyone give me an idea how to achieve that? </p> <p>Thanks !!! </p> <p><strong>Code Snippet:</strong> </p> <pre><code>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; } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload