JavaScript Basics Tutorial Data types (String strings) _ Basics

Source: Internet
Author: User

A. String

Copy Code code as follows:

var language = "JavaScript";
var language = ' JavaScript ';

Strings can be used in double quotes and single quotes, depending on your personal preference.

The string has a length property that can return the number of strings in a variable.

Copy Code code as follows:

var test1 = "Teacher";
document.write (test1.length);
Number of string output test1:7

Conversely, to get the character at the specified position, you can use the Charat () function (the first character is 0, the second character is 1, and so on).

Copy Code code as follows:

var test1 = "Teacher";
document.write (Test1.charat (1));
The results of the operation are: E,

If you want to get a string in a variable, you can use slice (), substring (), or the substr () function.

where substring () and slice () accept two parameters

Copy Code code as follows:

var test1 = "Teacher";
document.write (test1.substring (1) + "<br>");//Output Eacher
document.write (test1.substring (1,4) + "<br>"); Output EAC
document.write (Test1.slice (1,4) + "<br>"); Output EAC
document.write (Test1.slice (4) + "<br>"); Output her
document.write (test1 + "<br>");/full string

As you can see from the above, substring () and slice () do not change the contents of the string, only the contents of the string.

The main difference between substing () and slice () is that negative numbers are handled differently.

For slice (), negative numbers are counted forward from the end of the string, and for substring (), the negative numbers are ignored, processed from 0, and the smaller number in the two arguments as the starting bit and the larger end bit.

For example, substring (2,-3) is equivalent to substing (2,0), which is equivalent to substring (0,2).

Copy Code code as follows:

var test1 = "Teacher";
document.write (test1.substring (2,-3) + "<br>"); Te
document.write (test1.substring (2,0) + "<br>"); Te
document.write (test1.substring (0,2) + "<br>"); Te
document.write (Test1.slice (2,-3) + "<br>"); Ac
document.write (test1 + "<br>"); Teacher

The difference between substring () and substr () is illustrated.

Copy Code code as follows:

var Tt,ss;
var s = "hellobeijing";
tt = s.substring (2,8) + "<br>";
SS = S.substr (2,8);
document.write (TT);/output: Llobeij output subscript 2 to subscript 8 characters
document.write (ss); Output: Llobeiji (8 characters after the output subscript 2)

For use, another blogger has more instances (addresses)

On the search string, JavaScript provides indexof () and LastIndexOf () two functions.

Copy Code code as follows:

var s = "woaibeijing";
DD = S.indexof ("e") + "<br>";/In the past
EE = s.indexof ("E", 3) + "<br>";//optional arguments, looking back from the first few characters
FF = S.lastindexof ("e") + "<br>";
GG = S.lastindexof ("E", 3) + "<br>"; Optional arguments, looking forward from the first few characters
HH = S.lastindexof ("H") + "<br>";
document.write (DD);
document.write (FF);
document.write (EE);
document.write (GG);
document.write (HH);

In addition, the use of indexof () and LastIndexOf () is recommended for viewing this article. Http://www.jb51.net/article/44921.htm

The indexof and LastIndexOf in JS are a very useful function for handling strings, which are described below for their definition, usage, considerations, and recommendations for use.

1, Strobj.indexof (substring[, StartIndex])

Function: Returns the first occurrence of the specified substring within the source string the index value in the source string (the index value of the nth character in the source string is n-1) and is an integer.

Parameter meaning:

Strobj is the source string and must be selected.

SUBSTRING is a substring that is found in the source string object and must be selected.

StartIndex is the starting index value, the IndexOf function is to find the character from the index value of the source string startindex (that is, the first startindex+1 character), optional. When omitted, the character (that is, the 1th character) from the source string index value of 0 is searched.

Example Analysis:

Usage 1 (Do not specify startindex value): var i= "huoshandao.com". Indexofof ("a"): Then i=5
Equivalent to Var i= "huoshandao.com". IndexOf ("A", 0)
Usage 2 (Specify startindex value): var i= "huoshandao.com". IndexOf ("A", 6): Then i=8
Tip: You can use alert (i), the statement test results, the following examples are the same.

Attention matters

1) Strobj can be either a string or a string variable.
Example
Strobj is a string: var i= "huoshandao.com". IndexOf ("."):
Strobj is a string variable: var str= "huoshandao.com"; var i=str.indexof (".");
2 substring cannot be an empty string, if it is an empty string, the return value is 0, but it can be a space.
Example
Substring is an empty string: var i= "Huo Shan Dao". IndexOf (""): Then i=0
SUBSTRING is a space string: var i= "Huo Shan Dao". IndexOf (""): Then i=3
3 The startindex value of the 1th character is 0, is the minimum index value, the 2nd character's startindex value is 1, and the startindex value of the last character is the source string length minus 1, which is the maximum index value.
4 Returns-1 if no substring is found.
Example
var i= "huoshandao.com". IndexOf ("Huosan"): Then I=-1
5 If startindex is a negative number, the equivalent of startindex equals 0. If it is greater than the maximum index value, it is equivalent to startindex equal to the maximum index value.
Example
startindex is negative: var i= "huoshandao.com". IndexOf (".",-3); then i=10
with var i= "huoshandao.com". IndexOf (".", 0); The result is the same
StartIndex is greater than or equal to string length: var i= "Huoshandao.com_". IndexOf ("_", 16); then I=-1
with var i= "Huoshandao.com_". IndexOf ("_"); i=14

2, Strobj.lastindexof (substring[, StartIndex])

The IndexOf function is looking from left to right, but in practice we sometimes want the first character index value of a character or string to appear from right to left, in this case, JS gives another function LastIndexOf to solve this problem, using the method and IndexOf similar, It's just looking from right to left, and it's not going to repeat, simply give a few examples and compare it to indexof:

Example 1:var i= "huo.shan.dao.com". LastIndexOf ("."); Then i=12 and var i= "huo.shan.dao.com". IndexOf ("."); The i=3
Example 2:var i= "Huoshandao.com_". LastIndexOf ("_", 16) i=14 and var i= "Huoshandao.com_". IndexOf ("_", 16); then I=-1

3, the use of recommendations

To avoid unexpected results, unless there are special uses, the following guidelines are recommended:

1, startindex is non-negative, and is not greater than the maximum index value. If startindex is a variable, determine whether its value is within that range.
2, if the substring substring is a variable, first to determine whether it is empty and then use the IndexOf or LastIndexOf function.
3, the input substring should pay special attention to the difference between full-width and half-width characters.
4, note that indexof and LastIndexOf inside the case, JS is very sensitive to case. It is recommended to use Dreamweaver programming, if the function name of the case is wrong, the color is black, write to the words will become another color.

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.