Remove multiple spaces in a text field
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller
Remove multiple spaces in a text field
Hi
I have a text in a field and I would like all multiple spaces in the text.
Can anyone help?
Thanks
mm
I have a text in a field and I would like all multiple spaces in the text.
Can anyone help?
Thanks
mm
Re: Remove multiple spaces in a text field
MediaMan,
you could do it with this:
It should leave only one space instead of two or more spaces.
regards
Bernd
you could do it with this:
Code: Select all
on mouseUp
put field 1 into tdata
repeat with i = the number of chars in tData -1 down to 1
if char i of tData = space and char i + 1 of tData = space then delete char i + 1 of tData
end repeat
put tData into field 1
end mouseUp
regards
Bernd
Re: Remove multiple spaces in a text field
Hi Bernd
Looks like i got to go a little deeper into the manual – tried to use your code but couldn't get it to work – newbie probs -
Thanks anyway
mm
Looks like i got to go a little deeper into the manual – tried to use your code but couldn't get it to work – newbie probs -
Thanks anyway
mm
Re: Remove multiple spaces in a text field
MediaMan,
make a little teststack with one field and one button. Set the script of the button to the script I posted above.
Put some text into the field with multiple spaces between the words and at the end.
Then click the button. It works for me.
If you want to apply this script to an existing stack and have a named field, say
field "myFieldsName"
then in the script of the button you would replace 'field 1' with field "myFieldsName"
I don't see why it should not work.
If you delete anything numbered in a container you have to start from the end and go to the beginning because if you do it the other way around (going forward) and you test e.g. for char 5, delete char 5 then the former char 6 becomes immediately char 5 for which you already tested and the former char 6 escapes your testing.
What I do in the script is: I take the text of the field into a local variable 'tData'. Than I do the text changing in this local variable. It is a lot faster if you do this sort of things in a local variable instead of the field itself. Field access and deleting stuff repeatedly directly in fields is time consuming.
Just to show how you would do it directly in the field: this accesses the field many times and is not recommended.
What does not work for you?
regards
Bernd
regards
Bernd
make a little teststack with one field and one button. Set the script of the button to the script I posted above.
Put some text into the field with multiple spaces between the words and at the end.
Then click the button. It works for me.
If you want to apply this script to an existing stack and have a named field, say
field "myFieldsName"
then in the script of the button you would replace 'field 1' with field "myFieldsName"
I don't see why it should not work.
If you delete anything numbered in a container you have to start from the end and go to the beginning because if you do it the other way around (going forward) and you test e.g. for char 5, delete char 5 then the former char 6 becomes immediately char 5 for which you already tested and the former char 6 escapes your testing.
What I do in the script is: I take the text of the field into a local variable 'tData'. Than I do the text changing in this local variable. It is a lot faster if you do this sort of things in a local variable instead of the field itself. Field access and deleting stuff repeatedly directly in fields is time consuming.
Just to show how you would do it directly in the field:
Code: Select all
on mouseUp
repeat with i = the number of chars of field 1 -1 down to 1
if char i of field 1 = space and char i + 1 of field 1 = space then delete char i + 1 of field 1
end repeat
end mouseUp
What does not work for you?
regards
Bernd
regards
Bernd
Re: Remove multiple spaces in a text field
Bernd's code works fine for me. As stated above, the most likely issue is a field name problem.
If you just wanted to try a different method, you can use replaceText but it uses regular expressions which can be.. lets just call them a pain.
Heres the replaceText code
Good luck.
If you just wanted to try a different method, you can use replaceText but it uses regular expressions which can be.. lets just call them a pain.
Heres the replaceText code
Code: Select all
on mouseUp
put field 1 into tData -- puts your field content into a variable tData.
--If you have more than oen field make sure you point to the right one.
put replacetext( tData, " *"," ") into field 1
-- calls the replaceText function.
--The input string is tData , the regular expression is the 2nd parameter.
-- Its 2 spaces, which means find 2 spaces together. And an asterisk which means find any numvber
-- of the previous char in this case, space. The 3rd part is a single space like so. " "
--which replaces the matches found
--the return from the function is then put back into field 1.
end mouseUp
Re: Remove multiple spaces in a text field
Sturgis,
that is a lot more elegant then what I posted. I did it the hard way, but I shun away from regular expressions, I always manage to mess them up enough so that they dont work.
Thanks.
regards
Bernd
that is a lot more elegant then what I posted. I did it the hard way, but I shun away from regular expressions, I always manage to mess them up enough so that they dont work.
Thanks.
regards
Bernd
Re: Remove multiple spaces in a text field
I actually prefer your method just because it make sense to me. Regular expressions might as well be <insert language I don't understand here> to me in most cases.
bn wrote:Sturgis,
that is a lot more elegant then what I posted. I did it the hard way, but I shun away from regular expressions, I always manage to mess them up enough so that they dont work.
Thanks.
regards
Bernd
Re: Remove multiple spaces in a text field
Funny thing – you learned from each other and I learned from both of you – you're great guys – works on a new stack – something's messing up with the text on the old one – but I find out.
Thanks
mm
Thanks
mm
Re: Remove multiple spaces in a text field
MediaMan,
the only thing I can think of what is not working in your stack is that the field referencing is not working. Try this
In your stack name the field that conatains the text that you want to work on. Choose a name that is not a reserved word and that starts with a letter or an underscore. Just for this exercise call it myBigTextField. Then in the code I posted above whenever I wrote
you would change that to
If you have multiple fields on a card the numbering of a field can change if you change the layer of a field by sending it further back or to the front this changes the number of a field. You can see the number of a field or any other object in the properties inspector -> Size and Position. There you can also change the layering manually. So it is good practice to give objects a unique name and refer to that name in your scripts. And of course the button with your script has to be on the card where the field you want to work on is located. If not you would have to include the reference to the card or card and stack, if the field is in another stack, in your scripts.
tell us if it works.
regards
Bernd
the only thing I can think of what is not working in your stack is that the field referencing is not working. Try this
In your stack name the field that conatains the text that you want to work on. Choose a name that is not a reserved word and that starts with a letter or an underscore. Just for this exercise call it myBigTextField. Then in the code I posted above whenever I wrote
Code: Select all
field 1
Code: Select all
field "myBigTextField"
tell us if it works.
regards
Bernd