Reversing String separated by Comma

Posted by: dstauder

Reversing String separated by Comma - 04/23/19 03:59 PM

Hello all,

I need to reverse a LastName,FirstName in the data stream. I am using a Global Variable "global.ProviderName" to store the string.

Here is the vbscript that I am using:

****************************************************************
dim Str
dim reversedStr

str =Watch.GetVariable("global.ProviderName")
reversedStr =string.Join(",", str.Split(',').Reverse())

Watch.SetVariable("global.ProviderRev"), reversedStr

****************************************************************

It doesn't seem to like the ".Reverse()" command.

I can't seem to find a comparable command. Does anyone have a simple way to accomplish the task?

The Original text is formatted something like "Smith MD, John Adam"

Thank you in advance for the help,

Dan
Posted by: jim3108

Re: Reversing String separated by Comma - 04/26/19 11:10 AM

I'm sure you don't actually want to reverse the string as this would basically mirror it. If you used StrReverse on the string "james" for example, you would get "semaj".

Therefore, I will assume that you want the LastName after the FirstName.

Code:
Dim ArrStr
Dim OriginalStr
Dim NewStr
Dim x

OriginalStr = Watch.GetVariable("global.ProviderName")
ArrStr = Split(OriginalStr, ",")

For x = UBound(ArrStr) To LBound(ArrStr) Step -1
   NewStr = NewStr & ArrStr(x) & ", "
Next

Watch.SetVariable "global.ProviderRev", NewStr
Watch.Log OriginalStr & " reversed to: " & NewStr, 2


Give that a try...

NB: Did this without a test so let me know if you have trouble.
Posted by: Philippe F.

Re: Reversing String separated by Comma - 05/10/19 03:06 PM

Can't help myself, gotta use a regular expression. Here's a single JavaScript line of code that achieves the same thing:

Watch.SetVariable("global.ProviderRev", Watch.GetVariable("global.ProviderName").replace(/([^,]*)[,\s]*(.*)/,"$2 $1"));

Posted by: jim3108

Re: Reversing String separated by Comma - 05/11/19 05:18 PM

Very nice too, Sir.