Array of string turns

Source: Internet
Author: User

Tag: String new Split () will alert parameter describes a str solution

We know that by splitting a string into an array, JavaScript provides split ().

For example, here is the following string str1, which needs to be cut into [' 999 ', ' 999 ', ' 999 ']. That's easy.

<script>var str1 = "999,999,999"; var arr1 = S.split (","); // decompose at each comma (,). </script>

However, in practice, the processing of such a suitable string format is a bit idealized.

For example, when you come across a situation like this. The following string is also converted to [' 999 ', ' 999 ', ' 999 '].

var str2 = "999999999";

Then you will think that this requires a little bit of processing of the string. That is to insert a specific string (identifier) into the original string and process it into a string similar to our STR1 format.

The following is the core of what we are going to say.

<script>varstr2 = "999999999"; varNewstr= ""; //parameter Description: str denotes the original string variable, FLG represents the string to be inserted, and SN represents the position to be inserted     functionINSERT_FLG (STR,FLG,SN) { for(vari=0;i<str.length;i+=sn) {             varTmp=str.substring (i, i+SN); Newstr+=tmp+FLG; } newstr= Newstr.substring (0,newstr.length-1); returnNewstr; } insert_flg (STR2,', ', 3); varNewstr = Newstr.split (', '); alert (NEWSTR); </script>

In this way, we have solved the problem. The middle method of INSERT_FLG () is the key Oh.

Array of string turns

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.