unwanted space in string

Hi there,
I am trying to get some information from the source code in IE and use it in a variable, but when i run the script below i am getting a space in front of the string i wish to use. Is there any way to delete the unwanted space or use some kind of trim function??
Any help would be appreciated
thanks,

–here is the line in getsource that i am trying extract…
– p_peach

	tell application "Internet Explorer"
		Activate
			GetSource
			set longText to GetSource
			set targetpara to paragraph 113 of longText
	end tell

set startnum to targetpara
set new_num to replace_chars(startnum, "<TD>", "")
set new_num to replace_chars(new_num, "</TD>", "")
display dialog "this is the variable new_num:" & new_num

tell application "Internet Explorer"
	do script "window.document.forms[2].elements[38].value='" & new_num & "'"
	do script "window.document.forms[2].elements[40].click()"
	delay 3
end tell

on replace_chars(this_text, search_string, replacement_string)
set AppleScript’s text item delimiters to the search_string
set the item_list to every text item of this_text
set AppleScript’s text item delimiters to the replacement_string
set this_text to the item_list as string
set AppleScript’s text item delimiters to “”
return this_text
end replace_chars

There’s probably a better way to do this (I’m sure there are Scripting Additions that handle this, probably a Unix tool or two included with OS X, or just some better script), but here’s a quick script I just dashed off to trim strings:

Jon


[This script was automatically tagged for color coded syntax by Script to Markup Code]

I don’t know if it’s better than, worse than or equal to Jon’s offering but there’s some trimming code on this Apple page:

http://www.apple.com/applescript/guidebook/sbrt/pgs/sbrt.07.htm

– Rob

After looking at Apple’s code that Rob pointed out, I’ve modified my script a bit. My script is very similar to Apple’s except that mine attempts to trim any characters from a list of characters passed with the string to the handler instead of trimming a substring:

Jon


[This script was automatically tagged for color coded syntax by Script to Markup Code]

I think Jon’s approach is probably the best when you only want to trim leading and/or trailing white space and you don’t know what else may be in the string - such as punctuation or internal white space. Here’s a slight optimisation of his code:

set the_string to " this is a string    "
set the_string to (my trim_string(the_string, "both"))

on trim_string(the_string, trim_parameter)
  ignoring white space
    if the_string as string = "" then return "" -- the coercion makes this work with Unicode text too
  end ignoring
  
  set white_space to {space, tab, return, (ASCII character 10)} as string
  set start_char to 1
  set end_char to (count the_string)
  
  if trim_parameter is in "both left" then
    repeat while character start_char of the_string is in white_space
      set start_char to (start_char + 1)
    end repeat
  end if
  
  if trim_parameter is in "both right" then
    repeat while character end_char of the_string is in white_space
      set end_char to (end_char - 1)
    end repeat
  end if
  
  return text start_char thru end_char of the_string
end trim_string

Sorry, Jon. I see you’ve just posted pretty much the same thing yourself. :smiley:

No problem, NG. Actually, I meant to include one more optimization that you also hit on, trying to trap an error if the string passed to the handler only contains the characters to trim:

Jon


[This script was automatically tagged for color coded syntax by Script to Markup Code]

This is great, thanks heaps. :smiley: