Remove spaces from end of string

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   ";
to
apple = “A P P L E”;

Thanks in advance

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?

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.

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

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.


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.

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:

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

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.

ok… lets try this

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

Hi,

You can find a prototype that does it in:

http://www.layer51.com/proto/default.aspx

goto to the stings section.

Cheers

SHO

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

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:


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

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

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

Even ask Dan :stuck_out_tongue:

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. :stuck_out_tongue:

Lol, probably :slight_smile:

::bows to the supra god::