white space

I am trying to remove the white space within the the filename string, in applescript, but am not quite sure how to do it. I was trying to go through each character of the filename and, if the character wasn’t a whitespace, then append it to a new string variable, but I cannot figure out how to append. Any suggestions?

thanks

Here’s an example:

set testString to "h e l l o"
set newString to ""
repeat with theChar in (every character of testString)
	if (theChar as string) is not " " then set newString to newString & theChar
end repeat
return newString

Edit: Ugh. Use this instead:

set testString to "h e l l o"

set ASTID to AppleScript's text item delimiters
set AppleScript's text item delimiters to {" "}
set testString to text items of testString
set AppleScript's text item delimiters to {""}
set testString to "" & testString
set AppleScript's text item delimiters to ASTID

testString

By all means check out how to build a new string (an example of which has already been posted). However, for simply stripping spaces from a filename, this is both shorter and very much faster than iterating through each character (it assumes that AppleScript’s text item delimiters are set to the default of {“”}):

set currString to "Some File Name.txt"
set newString to currString's words as string
--> "SomeFileName.txt"

For much longer strings, this method is faster still:

set currString to "A Very Long String."
set text item delimiters to space
set newString to currString's text items
set text item delimiters to {""}
newString as string
--> "AVeryLongString."