JavaScript String Object

Source: Internet
Author: User
Tags what array

String Object

The string object is used to manipulate text (strings).

Syntax for creating a String object:
New String (s); String (s);
Parameters

The parameter s is the value to be stored in the string object or converted to the original string.

return value

When string () and operator new are used as constructors, it returns a newly created string object that holds the string representation of the string s or s .

When a string () is not called with the new operator, it only converts s to the original string and returns the converted value.

String Object method

1. Split () splits the string into an array of strings.

Grammar
Stringobject.split (separator,howmany)
Parameters Description
Separator Necessary. A string or regular expression that splits stringobject from where specified by the parameter.
Howmany Optional. This parameter specifies the maximum length of the returned array. If this parameter is set, the returned substring will not be more than the array specified by this parameter. If this argument is not set, the entire string is split, regardless of its length.
return value

An array of strings. The array is created by splitting the string stringobject into substrings at the boundary specified by separator . The string in the returned array does not include the separator itself.

However, if separator is a regular expression that contains a subexpression, the returned array includes the strings that match those sub-expressions (but not the text that matches the entire regular expression).

Hints and Notes

Note: If you use an empty string ("") as a separator, each character in the Stringobject will be split.

Note: The action performed by String.Split () is the opposite of what Array.join does.

Example 1

In this case, we'll split the string in different ways:

<script type= "Text/javascript" >var str= "How is you doing today?" document.write (Str.split ("") + "<br/>") document.write (Str.split ("") + "<br/>") document.write (str.split ("", 3)) </script>

Output:

How,are,you,doing,today? H,o,w,, A,r,e,, Y,o,u,, D,o,i,n,g,, T,o,d,a,y,? How,are,you

Example 2

In this case, we'll split the strings with more complex structures:

"2:3:4:5". Split (":")//will return ["2", "3", "4", "5"] "|a|b|c". Split ("|") will return ["", "a", "B", "C"]

JavaScript String Object

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.