Jump to content
Macro Express Forums

How to count the number of spaces in a string?


margaret

Recommended Posts

How would I go about counting the number of spaces in a string? The string will already have been trimmed on both sides, and will already have had any instances of two spaces in a row changed to one space. What I want to do is to evaluate the string, and if it only contains two words (hence one space), stop the macro, because that macro is designed to deal with a string of more than two words. I know how to find out if the string doesn't contain any spaces, but how would I find out if there's only one?

 

Thanks!

Link to comment
Share on other sites

Move your string to another text variable (T99, say) so the original won't be destroyed by the following.

 

Then

VARIABLE SET INTEGER N99 from position of text (space) in T99.

If N99 is zero there are no spaces at all in T99, so you are done.

If N99 is greater than zero, add 1 to counter.

VARIABLE MODIFY STRING replace substring " " with "x" in T99 (choose the option to replace just one instance, not all instances).

At this point you have counted the first space (if there is one), and then replaced it with the letter "x".

 

Now repeat the above until N99 is zero (all spaces have been counted and replaced by "x").

 

Whatever is in your counter is the number of blanks that were in the original string.

Link to comment
Share on other sites

Suppose your string is stored in T1.

 

Save the length of T1 into N1.

Replace, into T2, all spaces in T1 with nothing ("").

 

Save the length of T2 into N2.

 

Subtract, into N3, N2 from N1.

 

N3 now contains the number of spaces in T1, whether 0, 1 or more.

 

FYI, this works much faster with long strings in ME3 than in ME4 Pro!

Link to comment
Share on other sites

Suppose your string is stored in T1.

 

Save the length of T1 into N1.

Replace, into T2, all spaces in T1 with nothing ("").

 

Save the length of T2 into N2.

 

Subtract, into N3, N2 from N1.

 

N3 now contains the number of spaces in T1, whether 0, 1 or more.

 

This is great, Paul. Thanks! rberq' s solution works as well, but took me longer to write up. Naturally, I had it figured out his way before I saw yours...

Meg

Link to comment
Share on other sites

Is this still true after the variable commands were optimized in Macro Express Pro? Have you tried it with Macro Express Pro v 4.1.3.1 or later?

Definitely!

I wrote the following in MEP and in AutoIt.

 

- Read a text file into t1 (length of file is 11.7 MB)

- Save length of t1 into n1

- Replace all spaces in t1 with nothing

- Save length of t1 into n2

- Subtract n2 from n1 into n3

- Display n1, n2, and n3

 

In MEP, I aborted the macro after 5 minutes! Lines 1 and 2 executed instantly, Line 3 never completed.

In AutoIt, the entire script completed in 2 seconds and returned the correct answers.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...