stuff function implementation in c#

2020-02-14 01:02发布

I need to know any whether c# has any function equal to sql function stuff, which replace the input string into the original string based on the start and length given.

Edited for adding sample:

select stuff('sad',1,1'b')

select stuff(original string, start point, length,input string)

the output would be "bad".

标签: c# .net tsql
5条回答
在下西门庆
2楼-- · 2020-02-14 01:34

Taking Thorarin's example a little further, this function will handle out of range inputs and null strings.

    /// <summary>
    /// combines 2 strings by inserting the replacement string into the original 
    /// string starting at the start position and will remove up to CharsToRemove 
    /// from the original string before appending the remainder.
    /// </summary>
    /// <param name="original">original string to modify</param>
    /// <param name="start">starting point for insertion, 0-based</param>
    /// <param name="charstoremove">number of characters from original string to remove</param>
    /// <param name="replacement">string to insert</param>
    /// <returns>a new string as follows:
    /// {original,0,start}{replacement}{original,start+charstoremove,end}
    /// </returns>
    /// <example>
    /// "ABC".Split(1,1,"123") == "A123C"
    /// </example>
    public static string Splice(this string original, int start, int charstoremove,
                                string replacement)
    {
        // protect from null reference exceptions
        if (original == null)
            original = "";
        if (replacement == null)
            replacement = "";

        var sb = new StringBuilder();
        if (start < 0)
            start = 0;

        // start is too big, concatenate strings
        if (start >= original.Length)
        {
            sb.Append(original);
            sb.Append(replacement);
            return sb.ToString();
        }

        // take first piece + replacement
        sb.Append(original.Substring(0, start));
        sb.Append(replacement);

        // if new length is bigger then old length, return what we have
        if (start+charstoremove >= original.Length)
            return sb.ToString();

        // otherwise append remainder
        sb.Append(original.Substring(start + charstoremove));
        return sb.ToString();
    }
查看更多
欢心
3楼-- · 2020-02-14 01:36

There is no built-in method to do this, but you could write an extension method:

static class StringExtensions
{
    public static string Splice(this string str, int start, int length,
                                string replacement)
    {
        return str.Substring(0, start) +
               replacement +
               str.Substring(start + length);
    }

}

The usage is as such:

string sad = "sad";
string bad = sad.Splice(0, 1, "b");

Note that the first character in a string in C# is number 0, not 1 as in your SQL example.

If you wish, you can call the method Stuff of course, but arguably the Splice name is a bit clearer (although it's not used very often either).

查看更多
倾城 Initia
4楼-- · 2020-02-14 01:46

You could make an extension method that combines string.replace and string.insert

public static string Stuff(this string str, int start , int length , string replaceWith_expression)
{
    return str.Remove(start, length).Insert(start, replaceWith_expression);
}
查看更多
成全新的幸福
5楼-- · 2020-02-14 01:47

There's no such function in C#, but you can write it easily. Note that my implementation is zero-based (first character has index 0):

string input = "abcdefg";
int start = 2;
int length = 3;
string replaceWith = "ijklmn";
string stuffedString = input.Remove(start, length).Insert(start, replaceWith);
// will return abijklmnfg

You could also write an extension method, which makes it easier to use the function:

public static class StringExtensions
{
    public static string Stuff(this string input, int start, int length, string replaceWith)
    {
        return input.Remove(start, length).Insert(start, replaceWith);
    }
}

Usage:

string stuffed = "abcdefg".Stuff(2, 3, "ijklmn");
查看更多
别忘想泡老子
6楼-- · 2020-02-14 01:50

Use String.Insert() function both with String.Remove() function

"abc".Remove(1, 1).Insert(2, "XYZ") // result "aXYZc"
查看更多
登录 后发表回答