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