8

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

为添加示例而编辑:

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

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

输出将是"bad".

4

5 回答 5

9

String.Insert()函数与 String.Remove() 函数一起使用

"abc".Remove(1, 1).Insert(2, "XYZ") // result "aXYZc"
于 2013-04-12T09:58:13.163 回答
8

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

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名称更清晰一些(尽管它也不经常使用)。

于 2013-04-12T10:06:15.933 回答
1

你可以做一个扩展方法,结合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);
}
于 2013-04-12T10:06:15.200 回答
0

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");
于 2013-04-12T10:06:59.450 回答
0

再举个 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();
    }
于 2018-09-28T13:49:39.110 回答