Trying to extract the beginning of a string

Discuss macro implementations, ask for macro help (to share your creations, see User Creations, probably either Campaign Frameworks or Drop-in Resources).

Moderators: dorpond, trevor, Azhrei, giliath, jay, Mr.Ice

Post Reply
ecclektik
Kobold
Posts: 17
Joined: Thu Dec 12, 2019 11:50 pm

Trying to extract the beginning of a string

Post by ecclektik »

I am trying to make sense of the strfind function but am not having much success. What I am trying to accomplish is I have a string "[name] (Major)" and I want to save a new string with just "[name]", essentially, stripping off the " (Major)" from the end. [name] is a string consisting of letters and some of them have a special character like an ' or ! and it can be any length.

Maybe strfind is not the best way to do it and there is an easier way. Any ideas or suggestions?

User avatar
aliasmask
RPTools Team
Posts: 9024
Joined: Tue Nov 10, 2009 6:11 pm
Location: Bay Area

Re: Trying to extract the beginning of a string

Post by aliasmask »

Is name in []'s? Is Major in ()'s?

So, if name is any character, except ()'s, then you can just use ( as the list breaking point.

Code: Select all

[H: name = listget(myString,0,"(")]


ecclektik
Kobold
Posts: 17
Joined: Thu Dec 12, 2019 11:50 pm

Re: Trying to extract the beginning of a string

Post by ecclektik »

No, [Name] is not actually in [], but (Major) is in (). An example would be "Bad Eyes (Major)" and I want to remove "(Major)" so I wind up with just the string "Bad Eyes" or "Bad Eyes " is fine also if leaving the blank space would be easier. I am writing a macro to downgrade a major hindrance in SWADE to the minor version, or remove the hindrance if it is a minor already or has no minor/major versions. I have everything else working as far as I can tell except for this swapping part. The player selects the hindrance from a dropdown list which is populated from their current hindrances. The one they select is removed. Then I check to see if they have selected one with (Major) at the end. If they have, it should strip off the (Major), add (Minor), and use that to look to see if the minor version exists in the library token. If it does, it adds the minor one to the character.

ecclektik
Kobold
Posts: 17
Joined: Thu Dec 12, 2019 11:50 pm

Re: Trying to extract the beginning of a string

Post by ecclektik »

aliasmask wrote:
Sat Sep 26, 2020 2:23 am
Is name in []'s? Is Major in ()'s?

So, if name is any character, except ()'s, then you can just use ( as the list breaking point.

Code: Select all

[H: name = listget(myString,0,"(")]
Ohhh, I get it and that is creative. I didn't think listGet was what I needed as this wasn't a string list but I see you are designating the ( as the delimiter and treating it like a string list. If I set the delimiter as " (" instead, would that get rid of the blank space between "Name" and "(Major)"?

User avatar
Full Bleed
Demigod
Posts: 4736
Joined: Sun Feb 25, 2007 11:53 am
Location: FL

Re: Trying to extract the beginning of a string

Post by Full Bleed »

ecclektik wrote:
Sat Sep 26, 2020 4:31 pm
Then I check to see if they have selected one with (Major) at the end. If they have, it should strip off the (Major), add (Minor)
Looks like you can do this to do that:

Code: Select all

[h: name = "Tom (Major)"]
[r: newName = replace(name, "(Major)", "Minor")]
Maptool is the Millennium Falcon of VTT's -- "She may not look like much, but she's got it where it counts."

ecclektik
Kobold
Posts: 17
Joined: Thu Dec 12, 2019 11:50 pm

Re: Trying to extract the beginning of a string

Post by ecclektik »

That is an elegant solution too and combines both steps together, thanks!

Post Reply

Return to “Macros”