How to clear all spaces in a string

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.