Read about javascript string concatenation operator, The latest news, videos, and discussion topics about javascript string concatenation operator from alibabacloud.com
I recently read "Advanced guide for high-performance website construction", Chapter 7 is "Writing efficient JavaScript", and the author is Nicholas C. zakas (also author of JavaScript advanced programming) describes the optimization of string connections.
String connection is always one of the operations with the lowes
lengthThe Length property can return the number of characters in a string.Length is obtained based on the UTF-16 encoding of the string, with an empty string length of 0. Length cannot be modified.charAt ()The CharAt () method returns the character at the specified position. Note that JavaScript does not have a character data type that differs from the
String object
String object is used to process text (String ).Syntax for creating a String object:
new String(s);String(s);Parameters
ParametersSIs the value to be stored in the String
False, zero, null, undefined, and empty string objects in Javascript
In Javascript, We often come into contact with the five special objects mentioned in the question -- false, 0, empty string, NullAnd undefined. These objects are easy to use incorrectly, so you must be careful when using them. Type Detection
Let'
string object, if there is no match, returns -1,SUBSTR as the specified substring, startindex the integer value that indicates the start index position within the string object to find. If omitted, the lookup starts at the end of the string.var a= "ABCDEFGB"; Alert (A.lastindexof ("B")); 7 9, concat ()Returns a string that contains the
A string is defined in JavaScript by double or single quotation marks. the string object has only one property: The length property , which gets the lengths of the strings. Methods for handling the string itselfCharAt (Index)Returns a character at the specified position of index in the
Method of String 1. String: The contents of the enclosed or double quotation marks in JS are strings;var t = "true"; Console.log (typeof T); // "string"Console.log (typeoftrue); // "Boolean" var str = "Yyy./niha"; var s = ' www '; var str = "HelloWorld";2. Index: In the string, each character has a corresponding index,
There is no StringBuffer class in JavaScript when the situation is displayed, and a mainstream JavaScript StringBuffer class is implemented by prototype to construct a StringBuffer class.
Stringbuffer.js
Copy Code code as follows:
function StringBuffer () {
This.content = new Array;
}
StringBuffer.prototype.append = function (str) {
This.content.push (str);
}
StringBuffer.proto
string immutable, JS and no similar method. But we can simulate this buffer mechanism. The principle is to use an array of stitching, the source code is as follows:
Copy Code code as follows:
function StringBuffer () {
this.__strings__ = new Array ();
}
StringBuffer.prototype.append = function (str) {
This.__strings__.push (str);
return this; Convenient chain operation
}
StringBuffer.prototype.toString = function () {
In JavaScript, we are often exposed to the 5 more special objects mentioned in the topic--false, 0, empty strings, null, and undefined. These objects are easy to use, so you have to be careful when you use them.Type detectionLet's come down and see what their type is:[HTML]View Plaincopyprint?
script Type= "Text/javascript" >
alert (typeof (false) = = = ' Boolean ');
Alert (typ
item to the array (replace the deleted item)String properties:str.length; view string length;String method:Str.charat (index); Gets the corresponding character according to index;Str.concat (str2); concatenation of strings;Str.indexof (); Finds the first occurrence of a character subscript;Str.lastindexof (); Finds th
() method is invoked. At this point, the following steps are performed:
(1) Create a string that stores the results.
(2) Copy each string to the appropriate location in the result.
Copy Code code as follows:
function StringBuilder () {
This._string=new Array ();
}
Stringbuilder.prototype.append=function (str) {
This._string.push (str);
}
Stringbuilder.prototype.tostring=function
be manipulated like an array:You can also manipulate strings directly:If the value inside [] is not a number, or is out of range, or uses a letter:The string is similar to an array, and the string is an array-like object that cannot change a single character within a string. The string also cannot add a new property.A
Coding problem in JSHttps://www.haorooms.com/post/js_escape_encodeURIComponentI in the front-end JS Add time to create the DOM, there are Chinese characters, found to be garbled on the study of the nextFor character encoding javascript:escape("\u6625\u8282");//输出 "%u6625%u8282" javascript:unescape("%u6625%u8282");//输出 "春节" javascript:unescape("\u6625\u8282");//输出 "春节"For URL Chinese character codingencodeURIComponent()相比encodeURI()要更加彻底。decodeURI()JS string
Create a JSON object in JS:1. Defining JSON objects directlyvar employees ="firstName": "Bill", "LastName": "Gates""firstName": "George", "LastName": " Bush "firstName": "Thomas", "LastName": "Carter" }];alert (employees[0].firstname); You can see the output as Bill2. By stitching the string and then converting it to a JSON pair image.Example 1:1 varids=["1", "2", "3"];2 varnames=["One", "one", "three"];3 4 varJson= "[";5 for(vari=0;i){6json+={"id
followed by a newline character, not other characters (such as spaces), or it will be an error.Method Two: The Join operator ( + ) can concatenate multiple single-line strings to simulate a multiline string.var str = "Welcome come to" + "chongqing.welcome to" + "Chongqing"Third, length calculation str.lengthvar str = "Hello"; Console.log (str.length); // 5Four, string interception1, substr (A, b) the
This is a problem that came about two years ago when I was doing the first real site in my life.
The site uses a back-and-forth separation method that returns JSON data from the REST of the backend and renders the front-end to the page.
Like many beginner Javascript novices, I used to embed the JSON data in HTML in the form of a concatenation string. There is l
Both the string value of the original type and the object of the string class have the following properties and methodsLength property: The number of characters in this stringCharAt (number) Gets the string at the specified index positionConcat (String) concatenation of stri
Today, I read the blue ideal post and talked about a string Line Break Problem. Generally, if HTML code snippets are stored in the Code, the plus sign is used to connect strings.Of course this will be a little troublesome, so we have the following uncommon method:Var foo = 'xxx \Xxx ';The result of alert (foo) is:Add a few spaces to indent xxxxxx:Var foo = 'xxx \Xxx ';The result of alert (foo) is:XxxWhat if I don't fold it?Var foo = 'xxx \ XXX ';Alert
In JavaScript program writing process, according to different contexts, JS will automatically convert object to number or string before processing. The rules for this automatic conversion are as follows:
Object automatically converts to string rules:
1. If the object belongs to a class that is overridden by the ToString () method, the method is called. If t
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.