JavaScript: The Definitive Guide, Sixth Editio javaScript权威指南(第6版) pdf 文字版-文字版, javascript电子书, 和javascript 有关的电子书:

3.2.3 Working with Strings

One of the built-in features of JavaScript is the ability to concatenate strings. If you use
the + operator with numbers, it adds them. But if you use this operator on strings, it
joins them by appending the second to the first. For example:
msg = "Hello, " + "world";   // Produces the string "Hello, world"
greeting = "Welcome to my blog," + " " + name;
To determine the length of a string—the number of 16-bit values it contains—use the
length property of the string. Determine the length of a string s like this:
In addition to this length property, there are a number of methods you can invoke on
strings (as always, see the reference section for complete details):
var s = "hello, world"        // Start with some text.
s.charAt(0)                   // => "h": the first character.
s.charAt(s.length-1)          // => "d": the last character.
s.substring(1,4)              // => "ell": the 2nd, 3rd and 4th characters.
s.slice(1,4)                  // => "ell": same thing
s.slice(-3)                   // => "rld": last 3 characters
s.indexOf("l")                // => 2: position of first letter l.
s.lastIndexOf("l")            // => 10: position of last letter l.

s.indexOf("l", 3)             // => 3: position of first "l" at or after 3

s.split(", ")                 // => ["hello", "world"] split into substrings
s.replace("h", "H")           // => "Hello, world": replaces all instances
s.toUpperCase()               // => "HELLO, WORLD"
Remember that strings are immutable in JavaScript. Methods like  replace() and
toUpperCase() return new strings: they do not modify the string on which they are
In ECMAScript 5, strings can be treated like read-only arrays, and you can access in-
dividual characters (16-bit values) from a string using square brackets instead of the
charAt() method:
s = "hello, world";
s[0]                  // => "h"
s[s.length-1]         // => "d"
Mozilla-based web browsers such as Firefox have allowed strings to be indexed in this
way for a long time. Most modern browsers (with the notable exception of IE) followed
Mozilla’s lead even before this feature was standardized in ECMAScript 5.

友情链接It题库(| 版权归yishouce.com所有| 友链等可联系|粤ICP备16001685号-1