Best way to replace all spaces, symbols, numbers, uppercase letters from a string in actionscript?

半世苍凉 提交于 2020-01-14 11:51:50

问题


What would be the best way to simply take a string like

var myString:String = "Thi$ i$ a T#%%Ible Exam73@";

and make myString = "thiiatibleeam";

or another example

var myString:String = "Totally Awesome String";

and make myString = "totallyawesomestring";

In actionscript 3 Thanks!


回答1:


Extending @Sam OverMars' answer, you can use a combination of String's replace method with a Regex and String's toLowerCase method to get what you're looking for.

var str:String = "Thi$ i$ a T#%%Ible Exam73@";
str = str.toLowerCase(); //thi$ i$ a t#%%ible exam73@
str = str.replace(/[^a-z]/g,""); //thiiatibleexam

The regular expression means:

[^a-z] -- any character *not* in the range a-z
/g     -- global tag means find all, not just find one



回答2:


I think this is the regex you're looking for:

[Bindable]
var myString:String = "Thi$ i$ a T#%%Ible Exam73@";
[Bindable]
var anotherString:String = "";
protected function someFunction():void
{
    anotherString = myString.replace(/[^a-zA-Z]/g, "");
    anotherString = anotherString.toLowerCase();
}



回答3:


I belive what your looking for is:

var myString =  str.replace("find", "replace");

or in your case:

str.replace("$", "");

also, it might be:

str.replace('$', ' ');

//EDIT How about:

 var mySearch:RegExp = /(\t|\n|\s{1,})/g;

 var myString =  str.replace(mySearch, "");


来源:https://stackoverflow.com/questions/7031341/best-way-to-replace-all-spaces-symbols-numbers-uppercase-letters-from-a-strin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!