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.
Edited for adding sample:
select stuff('sad',1,1'b')
select stuff(original string, start point, length,input string)
the output would be "bad".
Taking Thorarin's example a little further, this function will handle out of range inputs and null strings.
There is no built-in method to do this, but you could write an extension method:
The usage is as such:
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 theSplice
name is a bit clearer (although it's not used very often either).You could make an extension method that combines
string.replace
andstring.insert
There's no such function in C#, but you can write it easily. Note that my implementation is zero-based (first character has index 0):
You could also write an extension method, which makes it easier to use the function:
Usage:
Use String.Insert() function both with String.Remove() function