JavaScript string Learning notes for common operation functions

Source: Internet
Author: User
Tags diff regular expression trim

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] + ' &lt;br/&gt; ';
 }
 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&lt;diff; i++) {
  filter = ' 0 ';
 }
 return filter + num;
}

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.