Jump to content
Macro Express Forums

Another string question


Recommended Posts

I am trying to figure out how to take text saved from the clipboard to a variable, and then process that text.  I was able to get the first part clipboard to variable , what I am stuck on is trying to separate the text. 

 

What I would like to do , if possible is take text form the the clip board ie.

 

one two three four  (the length of the clipboard text may vary from one time to the next)

convert it to:

 

x(1) = one
x(2) = two
x(3) = three

 

then to use x(1) in a different part of the code.

 

Any help or pointing in the right direction would help

 

Thanks

James

Link to comment
Share on other sites

Hi James,

 

One way to accomplish this is with the Split String instruction:

 

Variable Set String %x% to "one two three"
Split String "%x%" on " " into %y%, starting at 1
Text Box Display: %y[1]%
Text Box Display: %y[2]%
Text Box Display: %y[3]%

When run, the script displays "one" then "two" then "three".

 

When defining variable %y% I created it as an array with up to 100 elements.

Link to comment
Share on other sites

I would do the same as acantor but I would add code to ensure it doesn't crash if there are more strings than elements of the array. Two methods come to mind.

Count the number of elements in the string. To do this create a temporary variable as a copy of your clipboard string. Then remove the spaces. (Assuming there are no double spaces) then subtract it's length from the length of the original variable and add one. 

The other way is to use the error handler. Actually this should always be done for operations where a user could give you lord-knows-what. In the "on error" tab you will see 0x001E is the "Array is to Small". Use Catch Error commands to handle it. Help section is titled "Catch Error / End Catch Error" if you don't know how to do that yet. 

If those are problematic you can also chew through the string. What I have done in the past is to search for a string, often an element label in HTML but in your case it would be a space. Search for the position of that text. Grab the text up to that text (substring), evaluate it if need be, then delete that substring and repeat. Sometimes this iterative, ablative approach is quicker and less problematic. The exit of yoru loop can be condition met or when the position of the search result indicates no match. It's a safe way to process test without the overhead of error handling. 

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...