Abdul
Abdul

Reputation: 1476

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

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;

Upvotes: 1

Views: 177

Answers (6)

Oilid
Oilid

Reputation: 151

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));

Upvotes: 3

Kien Chu
Kien Chu

Reputation: 4895

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

Upvotes: 1

JohnyL
JohnyL

Reputation: 7132

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";

Upvotes: 3

DirtyBit
DirtyBit

Reputation: 16772

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

Upvotes: 1

Michał Turczyn
Michał Turczyn

Reputation: 37347

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 + " ";

Upvotes: 1

Serega
Serega

Reputation: 650

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

Upvotes: 1

Related Questions