c#中的Stuff函数实现

本文关键字:实现 函数 Stuff 中的 | 更新日期: 2023-09-27 18:08:43

我需要知道c#是否有任何函数等于sql函数stuff,它根据给定的开始和长度将输入字符串替换为原始字符串

编辑添加示例:

select stuff('sad',1,1'b')
select stuff(original string, start point, length,input string)

输出将是"bad".

c#中的Stuff函数实现

同时使用String.Insert()函数和String.Remove()函数

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

没有内置方法可以做到这一点,但是您可以编写一个扩展方法:

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,而不是SQL示例中的1。

如果您愿意,当然可以将该方法称为Stuff,但可以说Splice名称更清楚一些(尽管它也不经常使用)。

您可以创建一个结合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);
}

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

在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");