How to make a string unique?

Hi , I have a String valuable “newStr” which contains data as this:dim newStr as string

newStr = “test1,test2,test3,test1,test3, test10,”,

Now, I want to remove the duplicates elements such as"test1" and “test3” in this case, to make it a new string with unique elements.

Anybody knows how to achieve it? thanks a lot,

Subject: How to make a string unique?

newStr = Join(ArrayUnique(Split(newStr, “,”)), “,”)

I think that should work.

Phil

Subject: RE: How to make a string unique?

Hi Phil, thanks a lot for your help,I tried your codes, unformatunately, it does not work.

I think the ArrayUnique only works for array, in this case, the result of Split does not return an array.

If it is working, I would like it, as it is so simple codes.

Any idea to make it working? thanks again.

Subject: RE: How to make a string unique?

Well, that’s very weird, because Split() is definitely supposed to return an array. What do you think it is returning? Did you try breaking it up and looking at the intermediate results in the debugger? I.e.,

temp1 = Split(newStr, “,”)

temp2 = ArrayUnique(temp1)

newStr = Join(temp2, “,”)

-rich

Subject: RE: How to make a string unique?

Cleaver code and works like a charm.

Subject: @Implode(@Unique(@Explode)))

I’ll assume you are getting the string from a field.

If it’s a string and not a multi-value field, you can use @Explode to turn it into an array, the @Unique to get the unique values, then @Implode to turn it back into a string.

Function Language to the rescue!!!

If it is a multi-value field, then the suggestions by others will work.

Subject: How to make a string unique?

You can load them into and array and use arrayunique function then loop thru and rebuild the string

here is a link to help

http://www.codestore.net/help/help6_designer.nsf/f4b82fbb75e942a6852566ac0037f284/914eba4dc05ded6a85256c1c003fabc0?OpenDocument

or if you want to do this in formula language use @Unique

Subject: RE: How to make a string unique?

Don’t tell him to use Formula in Lotusscript, then we will just see more ugly code with Evaluate of complex formulas, and questions why it does not work, when it is easier to do in native Lotusscript. :slight_smile:

Another way to do it is to use a list. Since you can only have one listtag with a specific name, that is a great way to build unique data.

Something like this:

Dim unique List As String

Forall s in Split(newStr)

if IsElement(unique(s)) = False Then

uniqueStr = uniqueStr + s + “,”

unique(s) = s

End If

End ForAll

This may not be the most efficient way to solve this particular problem, but hopefully Peter learn a bit about lists and how powerful they can be this way.

Subject: RE: How to make a string unique?

Nobody but you mentioned using formulas in Lotusscript; Barry offered formula language as an alternative to LS after giving the more correct (in this case) ArrayUnique LS construction.

Subject: RE: How to make a string unique?

Hi Karl,Thanks a lot for your help, I tried your codes, unfortunately, it does not work.

Subject: RE: How to make a string unique?

You need to learn to:1) use the debugger

  1. post the error you get as well as the values of different variables

The code below works perfectly fine for me:

Dim newStr As String

Dim uniqueStr As String

Dim unique List As String

Dim tmp As Variant

newStr = “test1,test2,test3,test1,test3,test10,”

tmp = FullTrim(Split(newStr,“,”))

ForAll s In tmp

If IsElement(unique(s)) = False Then

uniqueStr = uniqueStr + s + “,”

unique(s) = s

End If

End ForAll

MsgBox uniqueStr

I had missed the second argument to the Split() function in my code, and the ForAll command could not handle the returned data on the fly, so I changed to use an intermediate variable.