Javascript Base String Object __java

Source: Internet
Author: User
String Object

Create a String Object

var string1 = new String ("Hello");
var string2 = new String (123);
var string3 = new String (123.456);

You can also do this:

var string4 = "Hello";
the properties and methods of a String object

Length Property

The Length property returns the number of characters in the string.
For example:

    var myname = new String ("Jeremy");
    document.write (myname.length);

Output: 6

IndexOf () and LastIndexOf () ———— find another string in a string

method is sensitive to case sensitivity.

If the string value to retrieve does not appear, the method returns-1.

The first of the parameters in IndexOf () and LastIndexOf () is a required argument, the string to retrieve, the second argument is an optional argument, and the indexOf default is to retrieve from the beginning, LastIndexOf the default to retrieve from the string.length-1 location

IndexOf ()

var str= "Hello world!"
document.write (Str.indexof ("Hello") + "<br/>")/Output 0
document.write (str.indexof ("World") + "<br/>" )/output-1

LastIndexOf ()

var str= "Hello world!"
document.write (Str.lastindexof ("Hello") + "<br/>")/Output 0
document.write ("World") + < br/> ")/output-1

SUBSTR () and substring () methods ———— extract substrings

The substr () method extracts a specified number of characters from the start subscript in a string.

var str= "Hello world!"
document.write (Str.substr (3,7))/output Lo Worl

The substring () method extracts characters from a string mediation between two specified subscripts. This method is used to include headers that do not contain tails.

var str= "Hello world!"
document.write (str.substring (3,7))/Output lo W 

toLowerCase () and toUpperCase () ———— conversion case

var txt= "Hello world!"
document.write (Txt.tolowercase ());//Hello world!
document.write (Xt.touppercase ());//HELLO world!

CharAt () and charCodeAt () methods ———— Select a character from a string

The method charCodeAt () is similar to the operation performed by the CharAt () method, except that the former returns the encoding of the character at the specified position, and the latter returns a string of characters subcode.

var str= "Hello world!"
document.write (Str.charat (1));//E
document.write (Str.charcodeat (1));//101

fromCharCode () method

fromCharCode () can accept a specified Unicode value, and then return a string.

document.write (String.fromCharCode (72,69,76,76,79));/HELLO 
document.write ("<br/>")
document.write (String.fromCharCode (65,66,67));//ABC

String methods There are some ways to go over the details again, and interested can refer to the documents of the consortium.

Reprint please indicate the source ....

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.