Can StringBuilder replace one instance?
-
23-09-2019 - |
Question
I've got a chunk of text in a StringBuilder object. I need to replace one substring with another. The StringBuilder Replace method can do this, but it replaces every instance of the substring and I only want to replace the first found instance. Is there a way to tell StringBuilder to only do one replace?
I'm sure I could code this up myself, I'm just wondering if there is a built in way to achieve this that I am currently missing.
Solution
Yes, but not directly. Two of the four overloads of Replace
work on a substring of the StringBuilder
contents, but you'll need to find the position of the first occurrence for that to work.
Here's the one you want:
http://msdn.microsoft.com/en-us/library/y1bxd041.aspx
Edit: Unfortunately, I don't see a way to find the first occurrence without calling ToString
on the StringBuilder
.
(Sorry for the VB, I have a VB project open.)
Dim orig As String = "abcdefgabcdefg"
Dim search As String = "d"
Dim replace As String = "ZZZ"
Dim sb As New StringBuilder(orig)
Dim firstOccurrence = sb.ToString().IndexOf(search)
If firstOccurrence > -1 Then
sb.Replace(search, replace, firstOccurrence, search.Length)
End If
OTHER TIPS
This is different way of doing this, but works fine
StringBuilder sb = new StringBuilder("OldStringOldWay");
int index = sb.ToString().IndexOf("New");
sb.Remove(index, "Old".Length);
sb.Insert(index, "New");
Another way could be using Extension Method
public static StringBuilder ReplaceOnce
(this StringBuilder sb, string toReplace, string replaceWith)
{
int index = sb.ToString().IndexOf("New");
sb.Remove(index, "Old".Length);
sb.Insert(index, "New");
return sb;
}
And call ReplaceOnce as follows
static void Main(string[] args)
{
StringBuilder sb = new StringBuilder("OldStringOldWay");
sb.ReplaceOnce("Old", "New");
}
If you know the location of the substring you want to replace (maybe by using IndexOf) you can use the overload of StringBuilder's replace.
public StringBuilder Replace(char oldChar,char newChar,int startIndex,int count);
Here is the IndexOf
method that I created long time ago :).
/// <summary>
/// Gets the index of a string from a given index with case option
/// </summary>
/// <param name="sb"></param>
/// <param name="text"></param>
/// <param name="startIndex"></param>
/// <param name="ignoreCase"></param>
/// <returns></returns>
public static int IndexOf(this StringBuilder sb, string value, int startIndex, bool ignoreCase)
{
int num3;
int length = value.Length;
int num2 = (sb.Length - length) + 1;
if (ignoreCase == false)
{
for (int i = startIndex; i < num2; i++)
{
if (sb[i] == value[0])
{
num3 = 1;
while ((num3 < length) && (sb[i + num3] == value[num3]))
{
num3++;
}
if (num3 == length)
{
return i;
}
}
}
}
else
{
for (int j = startIndex; j < num2; j++)
{
if (char.ToLower(sb[j]) == char.ToLower(value[0]))
{
num3 = 1;
while ((num3 < length) && (char.ToLower(sb[j + num3]) == char.ToLower(value[num3])))
{
num3++;
}
if (num3 == length)
{
return j;
}
}
}
}
return -1;
}
Another working version from Asad's work.
public static string ReplaceOnce(this string source, string toReplace, string replaceWith)
{
int index = source.IndexOf(toReplace);
if(index != -1)
{
source.Remove(index, toReplace.Length);
source.Insert(index, replaceWith);
}
return source;
}