Can StringBuilder replace one instance?

I have a piece of text in a StringBuilder object. I need to replace one substring with another. The StringBuilder Replace method can do this, but it replaces each instance of the substring, and I want to replace only the first instance found. Is there a way to tell StringBuilder to only replace one?

I’m sure that I could do it myself, I’m just wondering if there is a built-in way to achieve this, which I’m missing now.

+3
source share
5 answers

Yes, but not directly. Two of the four overloads Replacework on a substring of content StringBuilder, but you will need to find the position of the first occurrence for this.

, :
http://msdn.microsoft.com/en-us/library/y1bxd041.aspx

: , ToString StringBuilder.

( VB, VB.)

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
+3

,

        StringBuilder sb = new StringBuilder("OldStringOldWay");

        int index = sb.ToString().IndexOf("New");           

        sb.Remove(index, "Old".Length);
        sb.Insert(index, "New");

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

ReplaceOnce

static void Main(string[] args)
{
   StringBuilder sb = new StringBuilder("OldStringOldWay");
   sb.ReplaceOnce("Old", "New");
}
+1

, (, IndexOf), StringBuilder.

public StringBuilder Replace(char oldChar,char newChar,int startIndex,int count);
0

IndexOf, :).

/// <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;
}
0

Asad 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;
}
0

Source: https://habr.com/ru/post/1735062/


All Articles