Topic Options
#56958 - 04/23/19 03:59 PM Reversing String separated by Comma
dstauder Offline
OL User

Registered: 06/28/02
Posts: 74
Loc: St. Louis, Missouri USA
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

Top
#56969 - 04/26/19 11:10 AM Re: Reversing String separated by Comma [Re: dstauder]
jim3108 Offline
OL Expert

Registered: 04/19/10
Posts: 277
Loc: London, UK
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.

Top
#57018 - 05/10/19 03:06 PM Re: Reversing String separated by Comma [Re: dstauder]
Philippe F. Offline
OL Expert

Registered: 09/06/00
Posts: 1931
Loc: Objectif Lune, Montreal, Qc
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"));

_________________________
Technical Product Manager
I don't want to achieve immortality through my work; I want to achieve immortality through not dying - Woody Allen

Top
#57021 - 05/11/19 05:18 PM Re: Reversing String separated by Comma [Re: dstauder]
jim3108 Offline
OL Expert

Registered: 04/19/10
Posts: 277
Loc: London, UK
Very nice too, Sir.

Top