How to clear all spaces in a string:
About clearing the space at both ends of the string has been introduced, you can see how JavaScript removes the space at both ends of the string chapter, this chapter will explain how to remove all the spaces in the string, the code example is as follows:
<!DOCTYPE HTML> <HTML> <Head> <MetaCharSet= "Utf-8"> <Metaname= "Author"content= "http://www.softwhy.com/" /><Head><title>Ant Tribe</title><Scripttype= "Text/javascript"> functionignorespaces (string) {varTemp=""; String="'+string; Splitstring=String.Split (" "); for(i= 0; I<splitstring.length; I++) {Temp+=Splitstring[i]; } returntemp;} Window.onload=function(){ varBT=document.getElementById ("BT"); varMyText=document.getElementById ("MyText"); Bt.onclick=function() {Mytext.value=ignorespaces (Mytext.value); }}</Script></Head><Body><inputtype= "text"ID= "MyText" /><inputtype= "button"value= "click Filter"ID= "BT" /></Body></HTML>
In the above code, when you enter a string in the text box, click the button to clear all the spaces in the string, the following is a brief introduction to the implementation process:
I. Principle of implementation:
The input string is split with a space ("") in the split () function, converted to an array, and then concatenated with each of the elements, thus excluding the spaces in the string. Some friends here may have this question, if the string contains two or more than two spaces together, then is not able to completely clear the space, in fact, will not be in the array of spaces ("") into Empty ("").
Two. Recommended reading:
The 1.split () function can refer to the section of the Split () method of the JavaScript string object .
The original address is: http://www.softwhy.com/forum.php?mod=viewthread&tid=8244
For more information, refer to: http://www.softwhy.com/javascript/
How to clear all spaces in a string