在C#中的东西的功能实现(stuff function implementation in c#)

2019-08-22 06:40发布

我需要知道C#的任何是否等于SQL函数的任何功能stuff ,它更换输入字符串转换成基于给定的开始和长度原始字符串。

编辑的加样:

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

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

输出将是"bad".

Answer 1:

没有内置的方法来做到这一点,但你可以写一个扩展方法:

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

}

用法是这样:

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

需要注意的是,在C#中的字符串的第一个字符是数字0,而不是1,在你的SQL实例。

如果你愿意,你可以调用方法Stuff ,当然,但可以说的Splice名字有点更清晰的(虽然它不经常使用两种)。



Answer 2:

使用String.Insert()函数都与String.Remove()函数

"abc".Remove(1, 1).Insert(2, "XYZ") // result "aXYZc"


Answer 3:

你可以做的是结合了扩展方法string.replacestring.insert

public static string Stuff(this string str, int start , int length , string replaceWith_expression)
{
    return str.Remove(start, length).Insert(start, replaceWith_expression);
}


Answer 4:

有没有在C#中没有这样的功能,但你可以很容易地写。 请注意,我的实现是从零开始的(第一个字符的索引为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

你也可以写一个扩展方法,这使得它更容易使用的功能:

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

用法:

string stuffed = "abcdefg".Stuff(2, 3, "ijklmn");


Answer 5:

以Thorarin的例子远一点,此功能将处理超出范围的输入和空字符串。

    /// <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();
    }


文章来源: stuff function implementation in c#
标签: c# .net tsql