How to replace duplicates from a string but keep the first occurance only

Hi There,

I need your help please.

How will I replace duplicates in a string but keep the first occurance only. Example:
myString = Invoice Type|Vanula|Invoice|December| Invoice to be submitted according to Invoice type-priority|2022

I want to keep the first Invoice Type as this will always be constant but replace any Invoice or Invoice types found.

Thank you in advance

@ClaytonM, is it possible if you can help me out with the above please?

Hey,

I’m not sure I understand your question fully.

Normally, it is easier to change a set of data using it as an enumerable or array type value.

myString = "Invoice Type|Vanula|Invoice|December| Invoice to be submitted according to Invoice type-priority|2022"

Let’s say you have a string like your example:

If you split the string by the delimiter, then it becomes an array and you can use .Distinct to take the unique values, thus removing duplicates. Then, output it back to a string using .String.Join()

myString = String.Join("|", myString.Split("|").ToArray.Distinct)

To be more advanced, in addition to .Distinct, you can use .Where() which allows you to use complex conditions.

myString = String.Join("|", myString.Split("|").Where(Function(x) x.ToLower.Contains(variable1) ).ToArray.Distinct)

These are just examples in .net syntax, but C# is similar.

Please provide further details of your question, if these examples don’t completely solve your problem.

Thanks

1 Like

Hi @ClaytonM thank you for your response.

Unfortunately with the above I still see duplicates of Invoice types (as mentioned above I want to keep the first Invoice Type in the string but if there are other duplicates of “Invoice types” or “Invoice” the end goal is for me to replace it with another text or blanks.

So my end string goal I want to see would be as below(where the word Replaced is inserted):

myString = Invoice Type|Vanula|Invoice|December| Replaced to be submitted according to Replaced -priority|2022.

Thanks.