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

淺唱寂寞╮ 提交于 2019-12-20 06:15:36

问题


I have a string

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

How would I remove all characters AL, UR, UN or may be some more like that.

My string should look like this;

QADEER REHMAN KHALIL

Currently I am trying do like this;

List<string> list = new List<string> { "AL", "UR", "UN" };

foreach (var item in list )

{
    systemName = systemName.Replace(item, "");
}

This is also removing AL from KHALIL, how do I restrict this to not removing a word containg that characters.

Update: While adding spaces to words in List, will only remove words which has space before and after the word. and concatenate UR to following word. I am loading List of words which are to be removed from database;


回答1:


Try this please :

var name = "AL QADEER UR AL REHMAN AL KHALIL UN";
var list = new List<string> { "AL", "UR", "UN" };
name = string.Join(" ", name.Split(' ').ToList().Except(list));



回答2:


static void TestRegex()
{
    string name = "AL QADEER UR AL REHMAN AL KHALIL UN";
    // Add other strings you want to remove
    string pattern = @"\b(AL|UR|UN)\b";
    name = Regex.Replace(name, pattern, String.Empty);
    // Remove extra spaces
    name = Regex.Replace(name, @"\s{2,}", " ").Trim();
    Console.WriteLine(name);
}

UPDATE

You can generate the pattern this way:

// Generate pattern
var list = new List<string> { "AL", "UR", "UN" };
string pattern = @"\b(" + String.Join("|", list) + @")\b";



回答3:


var words = new[] { "AL", "UR", "UN" };
var arr = systemName.Split(new[] { " " }, StringSplitOptions.RemoveEmptyEntries).Except(words);
systemName = string.Join(" ", arr);



回答4:


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<string> list = new List<string> { "AL", "UR", "UN" };

foreach (var item in name.Split(new char[] { ' ', ',', '.' }, StringSplitOptions.RemoveEmptyEntries))
    systemName += list.Contains(item) ? "" : item + " ";



回答5:


I am loading that list from database, and can be change any time, how do use this in regex when changes occur

okay then, the length would always be 2?

no, but not be greater than 4

public static void Main()
{
    var input = "AL QADEER UR AL REHMAN AL KHALIL UN AAA BBB";
    Regex re = new Regex(@"\b\w{1,4}\b");
    var result = re.Replace(input, "");
    Console.WriteLine(result);
}

OUTPUT:

QADEER REHMAN KHALIL

dotNetFiddle




回答6:


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



来源:https://stackoverflow.com/questions/50208468/removing-word-or-character-from-string-followed-by-space-or-before-space-in-c-sh

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!