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

前端 未结 6 1223
悲哀的现实
悲哀的现实 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:32

    No need to use regular expressions. Having defined list with "prohibited" words, it's enough to iterate over wprds in the sentence to filter, if word is in the list of prohibited words, then exclude it, otherwise, include the word in final string.

    Try this:

    string name = "AL QADEER UR AL REHMAN AL KHALIL UN";
    string systemName = "";
    List list = new List { "AL", "UR", "UN" };
    
    foreach (var item in name.Split(new char[] { ' ', ',', '.' }, StringSplitOptions.RemoveEmptyEntries))
        systemName += list.Contains(item) ? "" : item + " ";
    

提交回复
热议问题