在C#中的东西的功能实现 [英] stuff function implementation in c#

查看:122
本文介绍了在C#中的东西的功能实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要知道的任何C#是否等于SQL函数 <$任何功能C $ C>的东西 ,该更换输入字符串转换成原始字符串基础上,开始和长度给出。

I need to know any whether c# has any function equal to sql function stuff, which replace the input string into the original string based on the start and length given.

主编的加样:

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

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

输出将坏。

推荐答案

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

There is no built-in method to do this, but you could write an extension method:

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

}

的用法是这样:

The usage is as such:

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

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

Note that the first character in a string in C# is number 0, not 1 as in your SQL example.

如果你愿意,你可以调用过程的方法的东西,但可以说是拼接名字有点更清晰的(虽然它不经常使用两种)。

If you wish, you can call the method Stuff of course, but arguably the Splice name is a bit clearer (although it's not used very often either).

这篇关于在C#中的东西的功能实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆