# Remove spaces from end of string

**URL:** <https://forum.kirupa.com/t/remove-spaces-from-end-of-string/8670>\
**Category:** flash\
**Created:** [November 26, 2002, 5:41am UTC](https://forum.kirupa.com/t/remove-spaces-from-end-of-string/8670 "2002-11-26T05:41:20Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![ask](https://avatars.discourse-cdn.com/v4/letter/a/df705f/32.png) [@ask](https://forum.kirupa.com/u/ask)\
**Post date:** [November 26, 2002, 5:41am UTC](https://forum.kirupa.com/t/remove-spaces-from-end-of-string/8670/1 "2002-11-26T05:41:20Z")

</div>

Need some help here. I asked dan4885 who is usually my action script guru, but he didn’t know how.

I want to remove spaces from the end of a string.

apple = "A P P L E&nbsp;&nbsp;&nbsp;";  
to  
apple = “A P P L E”;

Thanks in advance

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [November 26, 2002, 7:04am UTC](https://forum.kirupa.com/t/remove-spaces-from-end-of-string/8670/2 "2002-11-26T07:04:41Z")

</div>

hmm…

do you know ahead of time that it’s a white space or do you need a script that looks to see if there’s a white space and then removes it if it is there, or leaves the last character alone if it’s not a white space?

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [November 26, 2002, 7:15am UTC](https://forum.kirupa.com/t/remove-spaces-from-end-of-string/8670/3 "2002-11-26T07:15:45Z")

</div>

I was talking to Dan about this, and he said that the letters have spaces in between the, but all blank spaces after that should be removed, the catch is, the amount of blank spaces after that is random depending on the text :-\

I couldn’t figure it out.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [November 26, 2002, 8:10am UTC](https://forum.kirupa.com/t/remove-spaces-from-end-of-string/8670/4 "2002-11-26T08:10:18Z")

</div>

This is just an idea, I thought of this a bit after I posted this request for help. Not sure if it’ll work and I’m not a flash action scripter so its not using flash functions but this is just the logic.

Im thinking about using the splice function and splicing off two spaces at a time off the end of the string. The catch is if there is an odd number of spaces like 5 there will be one left. So at the end check if the last char is a space, and if so delete it.

I think this should work, because the characters won’t have more than one space in between them.

Any ideas? If somoene could convert my logic into AS id be very grateful. Perhaps make it a function. AS isn’t my strong point

Thanks

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [November 26, 2002, 8:34am UTC](https://forum.kirupa.com/t/remove-spaces-from-end-of-string/8670/5 "2002-11-26T08:34:12Z")

</div>

Well that’s not too difficult as long as the spaces between the letters can be assumed. We simply write the same stripwhite string method as usual, but set aside an if statement preventing it from removing the white spaces before it reaches the end.

This probebly wont work as it is. I’m just writing the concept down. I’m looking up the a/s where I can, but I haven’t programed in months, and I’m ‘cold’ on the real a/s code.

```php

function removeWhiteEnd(){
  for(i=0;i<this.length();i++){
     if(this.charAt(i)>196){
        trip=i;
     }
  this=this.slice(0,[trip]);
  }
}

```

the if statement judges to see if the character is a real character, rather than a space or symbol. If it is, it sets the trip to equal that number. then the last command clips off anything after the last letter in the string. All white spaces before that point are left alone.

There are two problems with the above script.  
A) in the if statement, I’m not sure what the number is. 196 comes to mind, but I can’t find the list online and I don’t have ASDG with me here at work. (dummy… you should never leave home without that).  
B) the slice command… I’m not sure of the syntax on that.

so if any of you guys could give me the proper methods, that script should work.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [November 26, 2002, 10:11am UTC](https://forum.kirupa.com/t/remove-spaces-from-end-of-string/8670/6 "2002-11-26T10:11:09Z")

</div>

I’m a bit cold too, but here’s an idea: since the problem is that you don’t know how many blank space there are (if I understood the problem), you can try to use a recursive function. Your function checks the last element of the string. If it’s a space, it is removed. And you run that function recursively until there is no space left at the end of the string. It should work unless there are more than 256 spaces (which should be enough :)).

Sounds good to you?

pom :asian:

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [November 26, 2002, 10:14am UTC](https://forum.kirupa.com/t/remove-spaces-from-end-of-string/8670/7 "2002-11-26T10:14:47Z")

</div>

Ad Upu, I’ve just seen your function. Won’t that remove all the blanks in the string?

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [November 26, 2002, 12:46pm UTC](https://forum.kirupa.com/t/remove-spaces-from-end-of-string/8670/8 "2002-11-26T12:46:02Z")

</div>

not if it continues to move up the string, leaving the trip set to the last none blank character.

Like i said… I need to tool it I think for it to work properly, but the idea should still be there.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [November 26, 2002, 1:16pm UTC](https://forum.kirupa.com/t/remove-spaces-from-end-of-string/8670/9 "2002-11-26T13:16:20Z")

</div>

ok… lets try this

```php
function removeEndWhite(userInput){
  for(i=0;i<userInput.length;i++){
     if(userInput.charAt(i)!=" "){
        trip=i;
     }
  output=userInput.slice(0,trip+1);
  }
}

```

In the case of this code I tested it with an input text field var ‘userInput’, a dynamic text field var ‘output’, and a button with the script

on(release){  
removeEndWhite(userInput);  
}

and it worked every time, no matter what I put into the text box… if Im reading the question right

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [November 26, 2002, 3:26pm UTC](https://forum.kirupa.com/t/remove-spaces-from-end-of-string/8670/10 "2002-11-26T15:26:18Z")

</div>

Hi,

You can find a prototype that does it in:

[http://www.layer51.com/proto/default.aspx](http://www.layer51.com/proto/default.aspx)

goto to the stings section.

Cheers

SHO

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [November 26, 2002, 6:04pm UTC](https://forum.kirupa.com/t/remove-spaces-from-end-of-string/8670/11 "2002-11-26T18:04:52Z")

</div>

Thanks david. My first shot at it was very similar to your code, except idiotically I put the slice inside my while() loop.

Cheers,  
Ashutosh

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [November 26, 2002, 8:23pm UTC](https://forum.kirupa.com/t/remove-spaces-from-end-of-string/8670/12 "2002-11-26T20:23:11Z")

</div>

y’all know i love string manipulations…

here’s my version. it will remove all spaces (spaces only, not line breaks et al) from the end of a string. i named it rtrim after a similiar function in php:

```auto

String.prototype.rtrim = function(){
	var l = this.length;
	while(this.indexOf(l--) == " "){}
	return this.substring(0,l);
}

```

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [November 27, 2002, 5:37am UTC](https://forum.kirupa.com/t/remove-spaces-from-end-of-string/8670/13 "2002-11-27T05:37:29Z")

</div>

ahhh… nice Supra. I was thinking about substring for shortening that script, but I couldn’t get it to work yet. 😛

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [November 27, 2002, 5:38am UTC](https://forum.kirupa.com/t/remove-spaces-from-end-of-string/8670/14 "2002-11-27T05:38:25Z")

</div>

I tried substring too, but I couldn’t get it to work.

Even ask Dan 😛

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [November 27, 2002, 5:42am UTC](https://forum.kirupa.com/t/remove-spaces-from-end-of-string/8670/15 "2002-11-27T05:42:53Z")

</div>

Surpa is a god… that’s all there is too it. I’m convinced that it’s actually Moock come down from the mount to help all us little Flashers. 😛

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [November 27, 2002, 5:45am UTC](https://forum.kirupa.com/t/remove-spaces-from-end-of-string/8670/16 "2002-11-27T05:45:06Z")

</div>

Lol, probably 🙂

::bows to the supra god::
