Commonly used methods for JavaScript strings_javascript tips
Get class:
1) Dynamic method:
charAt: Get the character at the specified position in the string. (Parameter: one, specify the character position to be obtained)
1, does not accept negative numbers, if it is a negative number, an empty string will be returned.
2, if no parameters are given, the character at position 0 is obtained by default.
3, only receives one parameter.
charCodeAt: Get the Unicode encoding of the character at the specified position in the string (parameter: one, specify the character position to get the character encoding)
1. Any character has a unique character encoding.
2, only receives one parameter.
Commonly used:
Number: 48 ~ 57
Underscore: 95
Space: 32
Tab character: 9
Lowercase letters: 97 ~ 122
Capital letters: 65 ~ 90
2) Static method:
fromCharCode: Returns the corresponding character according to the specified character code. (Parameter: any number)
1, can receive multiple parameters.
2. The writing method is fixed (static method): String.fromCharCode(); //Character encoding valid range: 0 ~ 65535 String is a string object
var str = '我是字符串'; alert( str.charAt( ) ); //'' 如果本身长度只有,却找str.charAt() 找不到也是空字符串'',~str.length-是合法范围。 alert( str.charAt( ) ); //'我' 默认不写是,找到第一个字符 alert( str.charAt() ); // '字' alert( ''.charAt( ) ); // alert( ''.charAt(,) ); // alert( str.charCodeAt( ) );// unicode编码 alert( ''.charCodeAt() ); // alert(String.fromCharCode( ,)); //'字味' 根据编码转换成字符(静态方法)多个字符用 , 隔开
Find class:
indexOf: Find the first occurrence of the specified substring in the string. (The first parameter specifies the substring to be searched; the second parameter specifies the position to start searching.)
1, search from front to back, starting from position 0 by default.
2. If found, return the first found position. If not found, return -1.
3, if the second parameter is negative, it will be treated as 0 by default
lastIndexOf: Find the last occurrence of the specified substring in the string. (The first parameter specifies the substring to be searched; the second parameter specifies the position to start searching.)
1, search from back to front, starting from the length - 1 position by default.
2. If found, return the first found position. If not found, return -1.
var str = 'www.baidu.com/'; alert(str.indexOf('bai')); // 从左往右找找到一个就返回不会再往右找了 alert(str.indexOf('m',)) // 从第位开始向右找 alert(str.indexOf('X')) //- 不存在的话结果是 - 表示没找到 alert(str.lastIndexOf('ww')); //
Interception class:
substring: Extract a substring in the specified range. (The first parameter specifies the starting position to be extracted; the second parameter specifies the end position to be extracted.)
1, the extraction range includes the starting position, but does not include the ending position.
2, you can omit the second parameter, which means extracting from the starting position to the end of the string
3. Before extraction, the sizes of the two parameters will be compared first, and then the parameter positions will be adjusted in order from small to large, and then extracted.
4, all illegal parameters will be automatically converted to 0.
5, if no parameters are given, the entire string will be returned directly by default.
slice: Extract a substring of the specified range. (The first parameter specifies the starting position to be extracted; the second parameter specifies the end position to be extracted.)
1, the extraction range includes the starting position, but does not include the ending position.
2, you can omit the second parameter, which means extracting from the starting position to the end of the string
3, will not compare the positions of the two parameters, let alone adjust the position
4, the parameter can be a positive number or a negative number, and all other illegal parameters will be converted to 0.
5, a negative number indicates the character position from the back of the string to the front, and the position of the last character is -1.
var str = '我是字符串'; alert(str.substring()); //'我是字符串' alert(str.substring(-,)); //'我' alert(str.substring()); //字符串 alert(str.substring(,)); //'我是' 与str.substring(,)是一样的。可以检测两个数,大的往后仍,小的往前仍。负数当成来处理。 alert(str.slice(,)); //空白 找不到,不交换位置 alert(str.slice(-)); //'符串' 负数就是从后面倒着往前数
Comparison type:
alert('I'>'you'); //true String comparison only compares the Unicode value corresponding to the first character, and the following characters are not compared.
Other categories:
alert(str.length); //Get the string length
split() //Cut the string into an array
Parameters: a specifies a separator to split the string.
1. If the separator is not specified, it will not be divided and will be stored directly in the array.
2. Based on the separator, store the values on the left and right sides of the separator into arrays.
3. The separator itself will not be stored in the array.
4. The delimiter can only be a substring that exists in the string.
5. In the view of split, two characters must be connected by an empty string.
6. When using an empty string to split an empty string, you will get an empty array.
var str = ''; alert( typeof str ); //string alert( typeof str.split() ); //object alert( str.split().length ); //[''] alert( str.split('') ); //['',''] alert( str.split('a') ); //[''] alert( str.split('') ); //['',''] alert( str.split('').length ); //['','','',''] alert( str.split('') ); //['','','',''] //'' 由五个 '' 加 四个字符组成 alert( str.split('') );//['',''] alert( str.split('') ); //['',''] alert( ''.split(' ').length ); //[''] alert( ''.split('').length ); //[] //特例,只有这种情况下 split 才会返回空数组。
trim() : 去除字符串首尾的所有空格。(字符串中间的空格会保留)。
html5的新方法,低版本浏览器不支持。
toUpperCase() : 把字符串全部转换成大写。(没有参数)
toLowerCase() : 把字符串全部转换成小写。(没有参数)
最后,所有的字符串方法,都不会修改字符串本身。
javascript字符串连接类
在我们写前端的js时,经常会出现将很多的字符串通过“+”拼接起来,然后挂载的某个DOM元素上。然而关于使用“+”来拼接字符串,在各浏览器下解析的效果,我就在此不做对比了,网上这类的对比很多。很多牛人都说通过使用js中的Array的join方法来拼接字符串效果很不错。为此在项目中写一个js类,用来统一处理字符串的拼接。
代码
//自定义的一个字符串连接类,用于拼接字符串,比"+"要提升性能function StringBuffer() { this._strs = new Array(); }StringBuffer.prototype.append = function(str) { this._strs.push(str); };StringBuffer.prototype.arrayToString = function() { return this._strs.join(""); };
而我们在使用该类时,可以直接通过如下方法:
var strBuff=new StringBuffer();strBuff.append("hello,");strBuff.append("Welcome to Javascript!");alert(strBuff.arrayToString());

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Detailed explanation of JavaScript string replacement method and FAQ This article will explore two ways to replace string characters in JavaScript: internal JavaScript code and internal HTML for web pages. Replace string inside JavaScript code The most direct way is to use the replace() method: str = str.replace("find","replace"); This method replaces only the first match. To replace all matches, use a regular expression and add the global flag g: str = str.replace(/fi

This tutorial shows you how to integrate a custom Google Search API into your blog or website, offering a more refined search experience than standard WordPress theme search functions. It's surprisingly easy! You'll be able to restrict searches to y

So here you are, ready to learn all about this thing called AJAX. But, what exactly is it? The term AJAX refers to a loose grouping of technologies that are used to create dynamic, interactive web content. The term AJAX, originally coined by Jesse J

This article series was rewritten in mid 2017 with up-to-date information and fresh examples. In this JSON example, we will look at how we can store simple values in a file using JSON format. Using the key-value pair notation, we can store any kind

Leverage jQuery for Effortless Web Page Layouts: 8 Essential Plugins jQuery simplifies web page layout significantly. This article highlights eight powerful jQuery plugins that streamline the process, particularly useful for manual website creation

Core points This in JavaScript usually refers to an object that "owns" the method, but it depends on how the function is called. When there is no current object, this refers to the global object. In a web browser, it is represented by window. When calling a function, this maintains the global object; but when calling an object constructor or any of its methods, this refers to an instance of the object. You can change the context of this using methods such as call(), apply(), and bind(). These methods call the function using the given this value and parameters. JavaScript is an excellent programming language. A few years ago, this sentence was

jQuery is a great JavaScript framework. However, as with any library, sometimes it’s necessary to get under the hood to discover what’s going on. Perhaps it’s because you’re tracing a bug or are just curious about how jQuery achieves a particular UI

This post compiles helpful cheat sheets, reference guides, quick recipes, and code snippets for Android, Blackberry, and iPhone app development. No developer should be without them! Touch Gesture Reference Guide (PDF) A valuable resource for desig
