Removing word or character from string followed by space or before space in c#

前端 未结 6 1171
悲哀的现实
悲哀的现实 2021-01-25 00:55

I have a string

string name = \"AL QADEER UR AL REHMAN AL KHALIL UN\";

How would I remove all characters AL, UR,

6条回答
  •  囚心锁ツ
    2021-01-25 01:23

    Pure LINQ answer with the help of EXCEPT

    string name = "AL QADEER UR AL REHMAN AL KHALIL UN";
    var list = new string[] { "AL", "UR", "UN" };
    
    name = name
       .Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries)
       .Except(list)
       .Aggregate((prev, next) => $"{prev} {next}");
    

    OUTPUT: QADEER REHMAN KHALIL

提交回复
热议问题