is there a way to remove all of the spaces in front of a piece of text? I need it to work for one or more spaces. Example:
" myname" or " myname"
into
“myname”
is there a way to remove all of the spaces in front of a piece of text? I need it to work for one or more spaces. Example:
" myname" or " myname"
into
“myname”
You can use “words of” or “paragraphs of” to eliminate the spaces and create a list. To get just one word, specify it by number. Very useful for formatting data returned by shell scripts.
word 1 of " myname"
WF
Using TextCommands:
tell application "TextCommands"
strip " myname" removing space from left end
end tell
--> "myname"
be careful - the word … of method does not only remove leading space: try for example:
word 1 of " -test" → results “test”, not “-test”
word 1 of " "test" → results “test”, not “"test”
etc.
an other way to remove leading space would be using sed or perl in a shell script:
set theString to " -test"
get (do shell script "echo " & quoted form of theString & " | sed 's|^\\ *||'")
set myQuestion to "What is your name?"
set res to text returned of (display dialog myQuestion default answer " WaltR")
repeat with i from 1 to number of words in res
set thisItem to word i of res
if thisItem is not " " then
set theWord to thisItem
end if
end repeat
display dialog theWord
Hi.
This vanilla method compares very favourably for speed and returns text of the same class as the original:
set someText to " -test " -- as Unicode text
set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to space
set TIs to someText's text items
set a to 1
set b to (count TIs)
repeat while (a < b) and ((count item a of TIs) is 0)
set a to a + 1
end repeat
(*
-- Only needed if stripping trailing spaces as well.
repeat while (b > a) and ((count item b of TIs) is 0)
set b to b - 1
end repeat
*)
set strippedText to text from text item a to text item b of someText
set AppleScript's text item delimiters to astid
return strippedText
the best way i’ve seen it done was listed as an essential subroutine on apple’s site
on replaceChars(thisText, searchString, replacementString)
set AppleScript's text item delimiters to the searchString
set the itemList to every text item of thisText
set AppleScript's text item delimiters to the replacementString
set thisText to the itemList as string
set AppleScript's text item delimiters to ""
return thisText
end replaceChars
search for " ", replace with “”
Z
I was not aware of this behavior. Thanks for pointing it out. I still wouldn’t hesitate to use this method if I knew that all the “words” started with letters of numbers, although some may disagree.
WF
Ahh;
But suppost the text to be stripped is: " -test bin " – as Unicode text
Then Nigel’s solution (with trailers enabled) returns “-text bin”
but Wolf’s returns “-testbin”
another attempt:
set someText to " -test bin" as Unicode text
set n to 1
repeat until character n of someText ≠space
set n to n + 1
end repeat
return (characters n thru -1 of someText) as Unicode text
And just reversing the string and running the loop again will eliminate following spaces:
set someText to " -test bin " as Unicode text
set n to 1
repeat until character n of someText ≠space
set n to n + 1
end repeat
set r to reverse of characters of someText as string
set m to 1
repeat until character m of r ≠space
set m to m + 1
end repeat
return (characters n thru -m of someText) as Unicode text
[One of our marvelous minimalists is invited to turn this into far fewer lines]
Hi Adam,
You must be reading my mind. What you can do instead is get the offset of the space if the wanted text contains no spaces.
set t to " myname"
set rev_cs to reverse of characters of t
set o to offset of space in (rev_cs as string)
set new_t to (reverse of (items 1 thru (o - 1) of rev_cs)) as string
I don’t know yet if it’s faster than any of the other methods.
gl,
Hi Adam,
one line (reversing the string) can be removed:
set someText to " -test bin " as Unicode text
set n to 1
repeat until character n of someText ≠space
set n to n + 1
end repeat
set m to -1
repeat until character m of someText ≠space
set m to m - 1
end repeat
return (characters n thru m of someText) as Unicode text
@ Kel,
interesting attempt - but it seems to fail at strings with trailing spaces ?
D.
for code minimalists: one more line of code reduced:
set someText to " -test bin " as Unicode text
return (characters getOffset(someText, 1) thru getOffset(someText, -1) of someText) as Unicode text
to getOffset(theString, theDirection)
set n to theDirection
repeat until character n of theString ≠space
set n to n + theDirection
end repeat
return n
end getOffset
Then there’s always:
set someText to " -test " as Unicode text
repeat while someText starts with space
set someText to someText's text 2 thru -1
end repeat
repeat while someText ends with space
set someText to someText's text 1 thru -2
end repeat