Basic Javascript tutorial-data type (String) _ Basics

Source: Internet
Author: User
Javascript has nine data types: String, numeric Number, Boolean, Undefine, Null, Object, reference Refernce, List, and Completion, let's take a look at it today (String) 1. String

The Code is as follows:


Var language = "javascript ";
Var language = 'javascript ';

Double quotation marks and single quotation marks can be used for strings, depending on your interests.

A string has the length attribute and can return the number of strings in the variable.

The Code is as follows:


Var test1 = "teacher ";
Document. write (test1.length );
// Number of output test1 strings: 7

Otherwise, you can use the charAt () function to obtain characters at the specified position (the first character is 0, the second character is 1, and so on)

The Code is as follows:


Var test1 = "teacher ";
Document. write (test1.charAt (1 ));
// The running result is: e,

If you want to obtain the string in the variable, you can use the slice (), substring () or substr () functions.

Both substring () and slice () accept two parameters.

The Code is as follows:


Var test1 = "teacher ";
Document. write (test1.substring (1) +"
"); // Output eacher
Document. write (test1.substring (1, 4) +"
"); // Output eac
Document. write (test1.slice (1, 4) +"
"); // Output eac
Document. write (test1.slice (4) +"
"); // Output her
Document. write (test1 +"
"); // Complete string

From the above content, both substring () and slice () do not change the string content, but only return the string content.

The difference between substing () and slice () is that the processing of negative numbers is different.

For slice (), the negative number is counted from the end of the string. For substring (), the negative number is ignored and processed from 0, the smaller numbers in the two parameters are used as the start bit, and the larger ones are used as the start bit.

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

The Code is as follows:


Var test1 = "teacher ";
Document. write (test1.substring (2,-3) +"
"); // Te
Document. write (test1.substring (2, 0) +"
"); // Te
Document. write (test1.substring (0, 2) +"
"); // Te
Document. write (test1.slice (2,-3) +"
"); // Ac
Document. write (test1 +"
"); // Teacher

The difference between substring () and substr (), for example.

The Code is as follows:


Var tt, ss;
Var s = "hellobeijing ";
Tt = s. substring (2, 8) +"
";
Ss = s. substr (2, 8 );
Document. write (tt); // output: The character between subscript 2 and subscript 8 is output by lobeij.
Document. write (ss); // output: lobeiji (8 characters after 2)

For usage, another blogger has more instances (addresses)

On the search string, Javascript provides two functions: indexof () and lastindexof.

The Code is as follows:


Var s = "woaibeijing ";
Dd = s. indexOf ("e") +"
"; // Past and back
Ee = s. indexOf ("e", 3) +"
"; // Optional parameter, which can be found after the specified character
Ff = s. lastIndexOf ("e") +"
"; // Forward from the back
Gg = s. lastIndexOf ("e", 3) +"
"; // Optional parameter, which can be found from the first few characters.
Hh = s. lastIndexOf ("H") +"
";
Document. write (dd );
Document. write (ff );
Document. write (ee );
Document. write (gg );
Document. write (hh );

In addition, we recommend that you refer to this article for the usage of indexof () and lastindexof. Http://www.jb51.net/article/44921.htm

IndexOf and lastIndexOf in JS are very useful functions for processing strings. The following describes their definitions, usage, precautions, and usage suggestions.

1. strObj. indexOf (subString [, startIndex])

Function: returns the index value of the source string (N-1 is the index value of the nth character in the source string) that appears for the first time in the source string. It is an integer.

Parameter description:

StrObj is a source string and is required.

SubString is a subString found in the source String object. It is required.

StartIndex is the start index value. The indexOf function is the character from the source string whose index value is startIndex (that is, startIndex + 1 character) to start searching. It is optional. If it is omitted, the source string index value is 0 characters (that is, 1st characters.

Instance analysis:

Usage 1 (without specifying the startIndex value): var I = "huoshandao.com". indexOfOf ("a"): Then I = 5
Equivalent to var I = "huoshandao.com". indexOf ("a", 0)
Usage 2 (specify the startIndex value): var I = "huoshandao.com". indexOf ("a", 6): Then I = 8
Tip: You can use alert (I); statement to test the result. The same is true in the following example.

Notes

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 a Null String. If it is a null 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 1st characters is 0, which is the minimum index value; The startIndex value of 2nd characters is 1; The startIndex value of the last character is the length of the source string minus 1, is the maximum index value.
4) if no substring is found,-1 is returned.
[Example]
Var I = "huoshandao.com". indexOf ("huosan"): Then I =-1
5) If startIndex is a negative number, it is equivalent to the case where startIndex is equal to 0. If it is greater than the maximum index value, it is equivalent to the case where startIndex is equal to the maximum index value.
[Example]
StartIndex is negative: var I = "huoshandao.com". indexOf (".",-3); then I = 10
Same as var I = "huoshandao.com". indexOf (".", 0 );
StartIndex is greater than or equal to the string length: var I = "huoshandao.com _". indexOf ("_", 16); then I =-1
And var I = "huoshandao.com _". indexOf ("_", 14); I = 14

2. strObj. lastIndexOf (subString [, startIndex])

The indexOf function is used to search from left to right, but in practice, we sometimes want to see the index value of the first character of a character or string from right to left. In this case, JS provides another function, lastIndexOf, to solve this problem. The method used is similar to indexOf, but it only searches from the right to the left. The specific method is no longer repeated. For example, compared with indexOf:

Example 1: var I = "huo.shan.dao.com". lastIndexOf ("."); then I = 12 and var I = "huo.shan.dao.com". indexOf ("."); I = 3
Example 2: var I = "huoshandao.com _". lastIndexOf ("_", 16); then I = 14 and var I = "huoshandao.com _". indexOf ("_", 16); then I =-1

3. Usage suggestions

To avoid unexpected results, we recommend that you follow the following principles unless otherwise used:

1. startIndex is a non-negative value and is not greater than the maximum index value. If startIndex is a variable, you must first determine whether its value is in this range.
2. If the subString is a variable, you must first determine whether it is null and then use the indexOf or lastIndexOf function.
3. When entering a substring, pay special attention to the differences between full-width and half-width characters.
4. Note the case sensitivity of indexOf and lastIndexOf. JS is very case sensitive. It is recommended to use Dreamweaver programming. If the function name is case-insensitive, the function color is black, and if it is correct, it 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.