Introduction to Strings
Enclosed in English single or double quotes, such as: ' Hello ', ' world ', but not the single and double quotes must be consistent, staggered, if you want to print single quotes or double quotes, you can use the escape character \ ' (single quotation mark), \ "(double quotes)
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: ' That's OK. "'; He said, "That's OK." " var str_4 = "He said: \" That's OK. \""; He said, "That's OK." " var str_5 = ' Hello world! '; Incorrect use of var str_6 = "Hello world!"; Incorrect use of
|
Connection string and another type of data
Use the + PLUS sign link string
The code is as follows |
Copy Code |
var uname = ' KimY '; var uage = ' 4 '; Alert (uname + ' this year ' + Uage + ' years old '); window: KimY is 4 years old this year. |
Connecting strings using the Concat () method
The code is as follows |
Copy Code |
var str = ' KimY '; var age = ' 4 '; var newstr = str. concat (' This year ', age, ' year old '); KimY 4 years old this year. alert (NEWSTR);
|
Condition comparison String
code is as follows |
copy code |
var strName = Prompt (' Please enter your name: ', '); if (strname.tolowercase () = = ' KimY ') { //== (double equals) compares whether the value of the variable strName is ' KimY ' alert (' You are KimY '); } else{ alert (' You are not kimy, you are ' +strname '); Strname.tolowercase () turns the string to lowercase and then compares |
Equal == operands are true, otherwise false
strict equality === operands are the same and have the same data type true; otherwise, false
is not equal! The number of = operands is true, otherwise the
is strictly unequal!== the operands are different or the data types are different, or false
greater than > the operand to the left is greater than
greater than or equal to >=
Less than
is less than or equal to <=
finds a substring in the string
indexOf () finds a substring from the start of the string, finds a substring, and returns the position of the substring's first letter , or return-1, and a return of 0 indicates a match at the start 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 starting from 0 } LastIndexOf () looks for a substring from the end of the string from right to left, can find a substring, returns the position of the substring's first letter, or returns-1 var pox = str.lastindexof (' key '); if (Pox!=-1) { Alert (' Found, index number: ' +pox '); Matches the key behind the slash } |
Extract substrings from a string
The code is as follows |
Copy Code |
var dststr = ' Hello mekkey! '; var start = Dststr.indexof (') +1;//gets the position of the first space and adds 1 var end = dststr.length;//Gets the length of the string var mystr = dststr.substring (start,end);//return substring by start position and end alert (mystr);//mekkey! Check for 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"//judge whether the variable Unknowkey exists typeof unknowkey.valueof () = = "string"///If Unkonwkey is a string object, returns a string direct quantity Unkonwkey.length > 0//Determine if the length of the string is empty var str = ' Helelo '; String Direct Quantity 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 a keyword string into a single keyword
Use the split () method to split the specified character, return an array, and the argument in the split () function can be either a string or a regular expression
The code is as follows |
Copy Code |
var str = "Http://www.111cn.net key=value"; var arr = str.split ('? '); /split string According to question mark in string Alert (arr[1]);//The window shows what follows the question mark |
To process a single row of textarea
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] + ' <br/> '; } resultstring + = ' </p> '; var result = document.getElementById (' result '); result.innerhtml = resultstring; } |
Before ECMAScript 5 is released, you must use a regular expression or a string replace () method to remove the trailing spaces of a string, which you can now only use the trim () method.
Str.trim ();
For browsers that do not support trim (), you can use string prototype to add a custom 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 left or right to add a string
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<diff; i++) { filter = ' 0 '; } return filter + num; } |