How to replace part of string by position?

后端 未结 18 894
逝去的感伤
逝去的感伤 2020-11-28 04:37

I have this string: ABCDEFGHIJ

I need to replace from position 4 to position 5 with the string ZX

It will look like this: ABC

18条回答
  •  Happy的楠姐
    2020-11-28 05:34

    As an extension method.

    public static class StringBuilderExtension
    {
        public static string SubsituteString(this string OriginalStr, int index, int length, string SubsituteStr)
        {
            return new StringBuilder(OriginalStr).Remove(index, length).Insert(index, SubsituteStr).ToString();
        }
    }
    

提交回复
热议问题