Replace text while keeping case intact in C#

后端 未结 2 1652
一个人的身影
一个人的身影 2021-02-15 02:08

I have a set of sentences i need to use to do a replace, for example:

abc => cde
ab df => de
...

And i have a text where to make the chan

2条回答
  •  一向
    一向 (楼主)
    2021-02-15 02:45

    You could use String.IndexOf with StringComparison.CurrentCultureIgnoreCase specified to find a match. At that point, a character by character replacement would work to do the swap. The capitalization could be handled by checking with Char.IsUpper for the source character, and then using Char.ToUpper or Char.ToLower on the destination as appropriate.

提交回复
热议问题