在c#中的东西函数实现

时间:2013-04-12 09:55:03

标签: c# .net tsql

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

编辑添加样本:

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

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

输出为"bad".

5 个答案:

答案 0 :(得分:8)

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

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

答案 1 :(得分: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名称更清晰一些(虽然它也不经常使用)。

答案 2 :(得分: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);
}

答案 3 :(得分: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");

答案 4 :(得分: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();
    }