Parsing a string containing capitalized words

I'm in need of the ability to parse and separate a text string. What I'd like to be able to do is provide the code a text string:
Put "MyTextIsHere" into exampleString

And then have spaces inserted before every capital letter save for the first, so the following is then stored in exampleString:
"My Text Is Here"

I basically want to separate the string into the words it contains. After searching the documentation and the web, I'm no closer to finding a solution to this.

Thank you in advance to anyone who can provide some insight!

Comments

  • Hi jamesmack1,

    There isn't a direct way to ask if a letter is a capital, but you can use the charToNum function to find the unicode number for a character, then check to see if that number is in the range of capital letters (65-90 for capital A-Z.)

    Here's a little script that does it:
    set startingString to "HereAreMyWords"
    set myRange to 2 to the number of characters in startingString //  The range to iterate over– every character except the first
    
    Put the first character in startingString into endString // The first character isn't included in the repeat loop, so you have to put it in separately
    
    repeat with each character myletter of characters myRange of startingString
    	if charToNum(myLetter) is between 65 and 90 // if the character's unicode number is between 65-90...
    		Put space after endString 
    	end if
    	Put myLetter after endString
    end repeat
    
    put endString
    
    Happy Testing,
    Pamela :)
  • Pamela wrote:
    Hi jamesmack1,

    There isn't a direct way to ask if a letter is a capital, but you can use the charToNum function to find the unicode number for a character, then check to see if that number is in the range of capital letters (65-90 for capital A-Z.)

    ...

    Great idea! Thank you so much.
Sign In or Register to comment.