-2

How can I overwrite a string ? Example:

string text = "abcdefghijklmnopqrstuvwxyz".OverwriteWith("hello world", 3);
// text == "abchello worldopqrstuvwxyz"

Of course this method doesn't exist. But

  • Is there something build in in .NET Framework ?
  • If not, how can I efficiently write a string into another string ?
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
Bitterblue
  • 13,162
  • 17
  • 86
  • 124

4 Answers4

5

You just need to use String.Remove and String.Insert method like;

string text = "abcdefghijklmnopqrstuvwxyz";
if(text.Length > "hello world".Length + 3)
{
   text = text.Remove(3, "hello world".Length).Insert(3, "hello world");
   Console.WriteLine(text);
}

Output will be;

abchello worldopqrstuvwxyz

Here a DEMO.

Remember, strings are immutable types in .NET. You can't change them. Even if you think you change them, you actually create a new string objects.

If you want to work with mutable strings, take a look at StringBuilder class.

This class represents a string-like object whose value is a mutable sequence of characters. The value is said to be mutable because it can be modified once it has been created by appending, removing, replacing, or inserting characters.

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
  • 1
    Make sure you test first if this string is long enough. If 3 + 11 is bigger than the length of the string, you'll get an ArgumentOutOfRangeException according MSDN: http://msdn.microsoft.com/en-us/library/d8d7z2kk.aspx – Ben Van Hees Aug 20 '13 at 07:22
  • 1
    @BenVanHees In OP case this is not necessary but added it. Thanks. – Soner Gönül Aug 20 '13 at 07:42
  • Thanks for stealing part of my answer without credit @SonerGönül :P – Gusdor Aug 22 '13 at 07:15
  • 1
    @Gusdor Do you think before your answer, I wasn't aware of the terms called `immutable`? Nice thought... – Soner Gönül Aug 22 '13 at 07:25
  • @SonerGönül I am questioning your integrity, not your knowledge. We communicated the same detail in the same order and same format with only a 3 word difference. Even disregarding the flagrant plagiarism, repetition of the information is redundant. Tell me, what prompted the edits? – Gusdor Aug 22 '13 at 07:41
5

Short answer, you cannot. Strings are an immutable type. This means that once they are created, they cannot be modified.

If you want to manipulated strings in memory, the c++ way, you should use a StringBuilder.

Gusdor
  • 14,001
  • 2
  • 52
  • 64
2

You can Try this Solution this may help you..

  var theString = "ABCDEFGHIJ";
  var aStringBuilder = new StringBuilder(theString);
  aStringBuilder.Remove(3, 2);  //Used to Remove the 
  aStringBuilder.Replace();  //Write the Required Function in the Replace
  theString = aStringBuilder.ToString();

Reference : Click Here!!

Community
  • 1
  • 1
coolprarun
  • 1,153
  • 2
  • 15
  • 22
1

What you want is an extension method:

static class StringEx
{
    public static string OverwriteWith(this string str, string value, int index)
    {
        if (index + value.Length < str.Length)
        {
            // Replace substring
            return str.Remove(index) + value + str.Substring(index + value.Length);
        }
        else if (str.Length == index)
        {
            // Append
            return str + value;
        }
        else
        {
            // Remove ending part + append
            return str.Remove(index) + value;
        }
    }
}

// abchello worldopqrstuvwxyz
string text = "abcdefghijklmnopqrstuvwxyz".OverwriteWith("hello world", 3);
// abchello world
string text2 = "abcd".OverwriteWith("hello world", 3);
// abchello world
string text3 = "abc".OverwriteWith("hello world", 3);
// hello world
string text4 = "abc".OverwriteWith("hello world", 0);
xanatos
  • 109,618
  • 12
  • 197
  • 280