Study Notes for common operation functions of JavaScript strings and javascript Study Notes

Source: Internet
Author: User

Study Notes for common operation functions of JavaScript strings and javascript Study Notes

String Introduction

Use single or double quotation marks, such as 'hello', "World", but the single quotation marks cannot start or end with the same as the double quotation marks. Use them in different ways. If you want to print single or double quotation marks, you can use the Escape Character \ '(single quotation marks), \ "(double quotation marks)

The Code is as follows: Copy code
Var str_1 = 'Hello World! '; // Hello World!
Var str_2 = "Hello World! "; // Hello World!
Var str_3 = 'He said: "This is acceptable. "'; // He said," This is acceptable. "
Var str_4 = "he said. \ ""; // He said: "This is acceptable. "
Var str_5 = 'Hello World! "; // Incorrect use
Var str_6 = "Hello World! '; // Incorrect usage

Connection string and another data type
Use the + plus sign to link the string

The Code is as follows: Copy code
Var uname = 'kimy ';
Var uage = '4 ';
Alert (uname + 'Year' + uage + 'Year'); // pop-up window: Kimy is 4 years old.

Use the concat () method to connect to a string

The Code is as follows: Copy code
Var str = 'kimy ';
Var age = '4 ';
Var newstr = str. concat ('Year', age, 'Year'); // kimy is 4 years old.
Alert (newstr );

Condition comparison string

The Code is as follows: Copy code
Var strName = prompt ('enter your name :','');
If (strName. toLowerCase () = 'kimy') {// = (double equal sign) compare whether the value of the variable strName is 'kimy'
Alert ('you are kimy ');
} Else {
Alert ('you are not Kimy, you are '+ strName );
}
StrName. toLowerCase () converts the string to lowercase, and then compares

Equal = the same number of operations is true; otherwise, the operation is false.
Strictly equal = the data type with the same operation count and the same operation count is true; otherwise, the operation is false.
Not equal! = The number of operations is true. Otherwise, the operation is false.
Strictly not equal! = True if the number of operations is different or the data type is different; otherwise, false.
Greater than> the number of operations on the left is greater
Greater than or equal to> =
Less than <
Less than or equal to <=
Search for a substring in a string
IndexOf () searches for the substring from the starting position of the substring. If you can find the substring, the position where the first letter of the substring is returned. Otherwise,-1 is returned, if the return value is 0, it indicates that it matches at the starting position.

The Code is as follows: Copy code
Var str = '111cn. net/key = value ';
Var pos = str. indexOf ('key ');
If (pos! =-1 ){
Alert ('found, index number:' + pos); // subscript index starts from 0
}
LastIndexOf () searches for substrings from right to left from the end of the string. If you can find the substrings, the position of the first letter of the substring is returned. Otherwise,-1 is returned.
Var pox = str. lastIndexOf ('key ');
If (pox! =-1 ){
Alert ('found, index number:' + pox); // match the key following the slash
}

Extract a substring from a string

The Code is as follows: Copy code

Var dstStr = 'Hello Mekkey! ';
Var start = dstStr. indexOf ('') + 1; // obtain the position of the first space and Add 1
Var end = dstStr. length; // obtain the string length.
Var myStr = dstStr. substring (start, end); // return the substring Based on the start position and end.
Alert (myStr); // Mekkey!
Check an existing non-empty string
If (typeof unknowkey! = "Undefined") & (typeof unknowkey. valueOf () = "string") & (unkonwkey. length> 0 )){
Alert ('unknowkey is a non-empty, existing string ');
}

Typeof unknowkey! = "Undefined" // determines whether the variable unknowkey exists.
Typeof unknowkey. valueOf () = "string" // If unkonwkey is a String object, a direct string volume is returned.
Unkonwkey. length> 0 // determines whether the string length is null.
Var str = 'helelo'; // number of direct strings
Var oStr = new String ('World'); // String object
Alert (typeof str. valueOf (); // output: String
Alert (typeof oStr. valueOf (); // output: String
Alert (typeof str); // output: String
Alert (typeof oStr. valueOf (); // output: Object

Break down a keyword string into separate keywords
The split () method is used to split by specified characters and an array is returned. The parameters in the split () function can be strings or regular expressions.

The Code is as follows: Copy code
Var str = "http://www.111cn.net? Key = value ";
Var arr = str. split ('? '); // Splits the string according to the question mark in the string
Alert (arr [1]); // The content following the question mark is displayed in a pop-up window.

Process a single row of textarea

The Code is as follows: Copy code
Function showResult (){
Var text = document. getElementById ('inputbox ');
Var lines = text. value. split ('\ n ');
Var resultString = '<p> ';
For (var I = 0; I <lines. length; I ++ ){
ResultString + = lines [I] + '& lt; br/& gt ;';
}
ResultString + = '</p> ';
Var result = document. getElementById ('result ');
Result. innerHTML = resultString;
}

Before the release of ECMAScript 5, you must use a regular expression or the String replace () method to remove spaces at the beginning and end of a String. Now you only need to use the trim () method.
Str. trim ();
For browsers that do not support trim (), you can use String prototype to add a customized trim method.

The Code is as follows: Copy code
If (typeof String. trim = 'undefined '){
String. prototype. trim = function (){
Return this. replace (/(^ \ s *) | (\ s * $)/g ,'');
}
}

Add a string to the left or to the right

The Code is as follows: Copy code

Function setSameLength (num, preLength ){
Var len = num. length;
Var diff = preLength-len;
Var filter = '';
For (var I = 0; I & lt; diff; I ++ ){
Filter + = '0 ';
}
Return filter + num;
}

From: http://www.111cn.net/wy/js-ajax/64285.htm


Javascript operation string

A statement, for example:
Var str = '1, 32
';

Var arr = str. split (',');

After execution, the arr in the array is 1, 2, and so on.

JavaScript: deletes a specified string from a string.

MymyFunc (a, B)
{
Return a. replace ("B ","");
}

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.