How to remove duplicate words from a string

Hi there,

I am running into a problem which I can’t fix.
There are some simular topics about it but for me it doesn’t do the trick.
In a string (strInput) I have the text: “CAT cat caterpillar S3 ESD”
I would like to remove the second ‘cat’
So the result must be:
CAT caterpillar S3 ESD

I tried:
String.Join(" “, strInput.Split(” “c).Distinct())
or
String.Join(” “, strInput.ToLower.Split(” "c).Distinct())

The first one doesn’t do anything and the second one works but returns the complete string in lowercases. I would like to keep the uppercases (CAT caterpillar S3 ESD)

Who is my hero of the day with the right solution?

Many, many thanks in advance!

grafik

1 Like

@bertjangroeneveld,

You can follow this approach:

Here is strOutput logic:

String.Join(" ", strInput.Split(" "c).Distinct(StringComparer.OrdinalIgnoreCase).ToArray())

Output:
image

Thanks,
Ashok :slight_smile:

Hero of the Day!!! :index_pointing_at_the_viewer:

1 Like

@bertjangroeneveld,

Cheers :beer:

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.