JavaScript string Operations Summary of some methods

Source: Internet
Author: User

Isfinite (123);//detection is a number (this returns TRUE)
isNaN (123);//detection is not a number (return false here)

' Hello World '. IndexOf (' A ', 2);//Find matching character position does not return-1, note the following optional parameters starting from a location, starting from 0

Cases

The code is as follows Copy Code

function Countinstances (MAINSTR, SUBSTR)
{
var count = 0;
var offset = 0;
Todo
{
offset = mainstr.indexof (subStr, offset);
if (offset!=-1)
{
count++;
Offset + + substr.length;
}
}while (offset!=-1)
return count;
}


' Hello World '. LastIndexOf (' A ', 2);//find matching character position from back to forward no return-1, starting from 0
' Hello World '. substring (1,4); the character that extracts the string intermediary between two specified subscripts, starting at 0
' Hello World '. substr (1,4);//extract a part of the string, starting from 0, the first argument, 1, is intercepted from the last, and the second parameter is length, starting from 0
' Hello World '. Slice (1,4);//extract a part of the string, starting from 0, if the first argument cannot be a negative number, and the second argument is null, all characters after the start of the Intercept, starting at 0
' Hello World '. Split (', 3);//Divide the string into an array of strings, and parameter 2 indicates the maximum length of the array

' Hello World '. Match (the "World");//Find character whether or not there is a string that returns the string does not measure null
/world/.test (' Hello World ');//Match character return true & False
' Hello World '. Search (/world/i);//Find the position of character, I ignore case
' Hello World '. Replace (/wor/, "Row");//Replace String
' Hello World '. length;//output String length

' Hello World '. toUpperCase ()//convert string to uppercase
' Hello World '. toLowerCase ()//convert string to lowercase
' Hello World '. valueof ()//input source code, such as Var a=function () {var str= "BBB";} Input out itself

' Hello World '. charAt (1);//returns the character at the specified position
' Hello World '. charCodeAt (1);//Unicode encoding of the character that returns the specified position
String.fromCharCode (72,69,76,76,79);//return the character at the specified position
' Hello World '. Concat (' caonima ')/link string
' Hello World '. FontColor (' #999999 ')//specified color to display string such as: <font color= #99999 >hello world</font>
' Hello World '. FontSize (' Caonima ')//uses the specified dimensions to display the string.

' Hello World '. Link ("http://www.classyuan.com");//Turn text to link

Nuances of IE6 and IE8 to JS string operations
originally thought JS code should be no problem, but later found in IE6 but not normal operation. Here is the page navigation of the JS code:

The code is as follows Copy Code

$ (function () {
$ ("#tr1 div"). Bind ("click", Function () {/) register an onclick event for all div inside the TR1
var name = $ (this). attr ("id"); Get ID

if (Name.indexof (' p ') = = "0")
{
for (Var k=1;k<15;k++)
{
var cid= "Child" +K;

if ((Name.length==7&&k==name.substring (6,7)) | | (name.length==8&&k== (name.substring (6,7) +name.substring (7,8)))
{
$ ("#" +cid+ ""). Show ();
}
Else
{
$ ("#" +cid+ ""). Hide ();
}
}
}
});
});

$ (function () {
$ ("#tr1 div"). Bind ("click", Function () {/) register an onclick event for all div inside the TR1
var name = $ (this). attr ("id"); Get ID

if (Name.indexof (' p ') = = "0")
{
for (Var k=1;k<15;k++)
{
var cid= "Child" +K;

if ((Name.length==7&&k==name.substring (6,7)) | | (name.length==8&&k== (name.substring (6,7) +name.substring (7,8)))
{
$ ("#" +cid+ ""). Show ();
}
Else
{
$ ("#" +cid+ ""). Hide ();
}
}
}
});
});

At the beginning of the IE8 browser, I directly get the first letter of name, directly using the subscript method of the array name[0], can be obtained in Google browser to run can, but I gave my colleagues when he was IE6 said did not reflect, I just started to think he was joking, Today, I use VS with the browser to browse the time to find that there is really a problem, depressed, that is jquery incompatible, looking for a long time to say that jquery compatibility is good, did not say compatibility is not good, I step by step debugging finally found in the judgment name[0]==p this problem found, So immediately change code name.substring (0,1), in the test, finally there is no problem, sometimes seemingly very small problem is really torture people! To remind you that when you make a Web page, if you need to use jquery, you should pay more attention to the problem of Cross-browser, as well as the coding problem, in the word escape

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.