Clarification on indexOf

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
taustinoc
Dragon
Posts: 518
Joined: Mon Aug 03, 2015 6:30 pm

Clarification on indexOf

Post by taustinoc »

If I'm using indexOf to find the first space in the text of a token property, is it going to strip off leading spaces? It appears that it does.

It appears this trimming occurs when I access the token property. If I type in the string:

[r:indexOf(" 79 74"," ")]

I get a result of 0, but if I access the token property:

[r:indexOf(TokenProperty," ")]

and TokenProperty equals " 79 74", I get a result of two.

Am I reading this right? And can I rely on this behavior? (It would be quite convenient if I can.)

It also appears to trim off any trailing space when I access a token property, too.

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

Re: Clarification on indexOf

Post by aliasmask »

In computer science, arrays and strings start with 0. It'll return -1 if not found.

taustinoc
Dragon
Posts: 518
Joined: Mon Aug 03, 2015 6:30 pm

Re: Clarification on indexOf

Post by taustinoc »

Yeah, I got that part. What I'm asking about is actually accessing token properties that start (or end) with a space. It appears that leading (and trailing) spaces are always trimmed off. Is that correct? I get the same indexOf for the first space for " 79 74" as I do for "79 74" - if it comes from a token property.

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

Re: Clarification on indexOf

Post by aliasmask »

I see. You are correct, there is some trimming going on that I don't think should be. But you can get the string with the spaces by using Wiki: getRawProperty().

taustinoc
Dragon
Posts: 518
Joined: Mon Aug 03, 2015 6:30 pm

Re: Clarification on indexOf

Post by taustinoc »

Trimming trailing spaces doesn't surprise me. Leading spaces does, a little, since they appear in the pop-up when you hover the mouse over it.

In this case, however, it's actually easier for me if it does trim leading spaces reliably. Some tokens, the properties will have leading spaces, some don't.

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

Re: Clarification on indexOf

Post by aliasmask »

I wouldn't rely on MT to do this since it's unusual behavior. If you want something trimmed I would use the trim function as well.

taustinoc
Dragon
Posts: 518
Joined: Mon Aug 03, 2015 6:30 pm

Re: Clarification on indexOf

Post by taustinoc »

I'm not sure it's bad that it does this, or that it should change, since there is a way to get the raw text (as you noted). Even so, yeah, I think I'll use trim (which I had not yet discovered until now). Thank you.

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

Re: Clarification on indexOf

Post by aliasmask »

It may be convenient for you, but anything that changes data unexpectedly is a bad idea in general. It's just one of those tricks you have to know about MT.

taustinoc
Dragon
Posts: 518
Joined: Mon Aug 03, 2015 6:30 pm

Re: Clarification on indexOf

Post by taustinoc »

I think the issue is more "unexpectedly" than "changes the data." If it's documented (and maybe it is, I didn't actually look), it's expected behavior. I really have no preference either way, personally, since there is a way to get the property text without any changes. But I'm just barely scratching the surface on what macros can do, so this may well be an important issue to some (and you'd know that better than me).

I did add in a Trim command, in case it changes in the future.

Post Reply

Return to “Macros”