node. js (v) string conversion

Source: Internet
Author: User

basic usage of 1.stringify functions

The purpose of the Stringify function is to serialize the object, that is, to convert the object type to a string type (the default delimiter ("&") and the quantifier ("="), and this section first describes its basic usage, and in the next section we will learn how to replace the default Let's take a look at the following examples to understand clearly!

Example 1:querystring.stringify ("Object")

  1. var querystring= require(' querystring ');
  2. var result = querystring. Stringify({foo:' Bar ',cool: [' Xux ', ' Yys ']});
  3. Console. Log(result);
Operation Result:
    1. Foo=bar&Cool=xux&Cool=yys
Multi-parameter usage of 2.stringify functions

In this section we will learn the multi-parameter usage of the Stringify function, the last section we know that the object is serialized as a string after the default is by the delimiter ("&") and the allocation ("=") of the composition, that can be changed, this section we come to understand, whether you could define the combination of the results, Look at the following small example

Example 1:querystring.stringify ("Object", "delimiter", "quantifier")

  1. var querystring = require(' querystring ');
  2. var result = querystring. Stringify({foo:' Bar ',cool: [' Xux ', ' Yys ']},' * ' ,' $ ');
  3. Console. Log(result);
Operation Result:
    1. ' Foo$bar*cool$xux*cool$yys '
basic usage of 3.parse functions

We have just learned the role of the Stringify function, and then we will learn the--parse function of the deserialization function, the function of the parse function is to deserialize the string (by default, "=", "&" stitching), the conversion to get an object type. The following example:

Example 1:querystring.parse ("string")

    1. var querystring = require(' querystring ');
    2. var result = querystring. Parse(' Foo=bar&cool=xux&cool=yys ');
    3. Console. Log(result);
Operation Result:
    1. { foo: ' Bar ', cool: [' Xux ', ' Yys ']}
Multi-parameter usage of 4.parse functions

Now that we are learning the extended use of the parse function, unlike the multi-parameter usage of the Stringify function in the previous section, the parse function can deserialize the string according to the user's custom delimiter and the delimiter, thus obtaining the corresponding object result. The following example:

Example 1:querystring.parse ("string", "delimiter", "quantifier")

    1. var querystring = require(' querystring ');
    2. var result = querystring. Parse(' [email protected][email protected][email protected] ',' @ ',' $ ');
    3. Console. Log(result);
Operation Result:
    1. { foo: ', bar: ' cool ', xux: ' cool ', yys : ' }

node. js (v) string conversion

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.