【问题标题】:stuff function implementation in c#c#中的stuff函数实现
【发布时间】:2013-04-04 19:10:00
【问题描述】:

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

为添加示例而编辑:

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

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

输出将是"bad".

【问题讨论】:

标签: c# .net tsql


【解决方案1】:

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

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

【讨论】:

  • OP 正在谈论 msdn.microsoft.com/en-gb/library/ms188043.aspx。字符串插入在这里不起作用。
  • @Yahya 这不是 String.Replace - 它是 String.Insert,它的作用与 Sql STUFF 的作用完全相同
  • Stuff 需要另一个参数来替换字符数,而 Insert 没有
  • 不完全一样。 STUFF 删除 length 字符,然后插入 input string,您的答案没有这样做。它只添加了input string
  • @RenéWolferink 是的,你说实话,我错了删除
【解决方案2】:

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

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

【讨论】:

  • 我创建了一个修改版本,添加了错误处理以避免下面的 null//range 异常。
【解决方案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);
}

【讨论】:

  • 为了提高效率,我会避免使用String.Remove 创建中间字符串结果,就像我在回答中所做的那样。
【解决方案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");

【讨论】:

    【解决方案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();
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多