JavaScript Learning-String Cut lookup

Source: Internet
Author: User

JavaScript string cut split

The most common method of string cutting in JavaScript is split() this function. This function returns an array after the cut.
Example:

var"hello world";var arr = str.split(" "//用空格切割字符串。alert( arr[0// 输出 helloalert( arr[1//输出 world

This is relatively simple.

Substring

This is also used to cut strings, but based on location, rather than character characteristics.
Example:

"iamalps1992"sub = str.substring(7, 11); //获取第7个字符到第11个(不包括第11个字符)的字符串alert(sub); //输出:1992

This is useful in many cases, especially when intercepting a part of a string.

IndexOf

This is the method of finding, which substring provides parameters to the method above. Returns the index subscript for the first occurrence of the found position.

Example:

var"Iamalps1992";var first = str.indexOf("a"// first的值为 1var second = str.indexOf("alps"// second的值为 3var third = str.indexOf("z"// third的值为 -1

The above is the way JavaScript cuts strings.

JavaScript Learning-String Cut lookup

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.