How to convert strings and arrays in js _ javascript tips-js tutorial

Source: Internet
Author: User
This article describes how to convert strings and arrays in Javascript. If you are interested, you can refer to a recent js interview question, which is described as follows: use var s1 = prompt ("enter any string", "") to obtain user input
String, try programming to "reverse" the user input string, and output the string.
After thinking about it, the string object method does not implement inversion, but there are arrays, so the conversion between strings and arrays is considered.
The built-in objects in JS include String objects and Array objects. These two objects can be implemented through their methods.
Converted. For String objects, the attribute and method of String operations are provided. For Array objects, the attribute and method of Array operations are provided.
Method. Therefore, it is easy to implement the above interview questions.
1. Convert strings into Arrays
Split () method in String object
The function of the preceding method is to cut a string into several segments and return an array. That is to say, a string can be converted into a numerical value. For example:
StrObj. split (split number). The parameter is a string of the split number. The specified split number is used to cut the string into several segments.
Instance:

// Specify the day of the week to be output. // define a weekly string var str = "Sunday, Monday, Tuesday, Wednesday, Thursday, Saturday "; // create a Date object var today = new Date (); // use the getDay () method of the today object var week = today. getDay (); // splits the week string into an array var arr = str. split (","); document. write ("type:" + typeof (arr) + ", the first element of the array is:" + arr [0] +"
"); // Output result document. write (" today is: "+ arr [week]);

The output result is: object type. The first element of the array is Sunday.
Today is: Friday
2. array conversion to string and array element Inversion
Join () method of Array object
The function of the preceding method is to convert an array into a string. For example, arrObj. join (connection number) links an array with the specified connection number into a word.
String.
Instance:

Var arr = ["a", "B", "c"]; var str = arr. join ("-"); document. write ("type:" + typeof (str) + ", string:" + str );

The output result is: string type, and the string is a-B-c.
Array object reverse () method
The function of the preceding method is to reverse the order of elements in the array. For example, arrObj. reverse ().
Instance:

var arr=["a","b","c"]; arr.reverse(); document.write(arr); 

The output result is: c, B,
Finally, let's look at the implementation code of the interview question:

/* Use var s1 = prompt ("enter any string", "") to obtain the user-input string, and try programming to "reverse" the user-input string ", and output the string. * /// Accept the string var s1 = prompt ("enter any string", ""); // convert the string to an array var arr = s1.split (""); // use the array object's reverse () method to reverse the arr. reverse (); // use the join () method of the array to convert to the string var str = arr. join (""); document. write (str );

Implementation result:

The above is all the content of this article, hoping to help you learn.

Related Article

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.