Common JS String Methods
来源:互联网 发布:当前大数据发展趋势 编辑:程序博客网 时间:2024/04/29 22:24
IndexOf(), getting the first occurrence of a specified text in string. JavaScript counts positions from zero.
<!DOCTYPE html><html><body><p id="p1">Please locate where 'locate' occurs!.</p><button onclick="myFunction()">Try it</button><p id="demo"></p><script>function myFunction() { var str = document.getElementById("p1").innerHTML; var pos = str.indexOf("locate"); document.getElementById("demo").innerHTML = pos;}</script></body></html>
<!DOCTYPE html><html><body><p id="p1">Please locate where 'locate' occurs!.</p><button onclick="myFunction()">Try it</button><p id="demo"></p><script>function myFunction() { var str = document.getElementById("p1").innerHTML; var pos = str.search("locate"); document.getElementById("demo").innerHTML = pos;}</script></body></html>
slices out a portion of a string from position 7 to position 13
<script>var str = "Apple, Banana, Kiwi";document.getElementById("demo").innerHTML = str.slice(7,13);</script>If a parameter is negative, the position is counted from the end of the string.
<span style="font-size:12px;"><script>var str = "Apple, Banana, Kiwi";document.getElementById("demo").innerHTML = str.slice(-12,-6);</script></span>If the second parameter is omitted, the method will slice out all the rest string. Or counting from the end.
var res = str.slice(7);var res = str.slice(-12);
substring() is similar to slice(), but can not accept negative values.
substr() is similar to slice(), the difference is that the second parameter specify the length of the extracted text.
replace() method replaces a specified value with another value in a string.
var n = str.replace("orginal", "replaced");toUpperCase() convert string to upper case.
var str = "Hello world!"var N = str.toUpperCase(); //returns HELLO WORLD!toLowerCase() is similarly used.
concat() joins two or more strings together.
var text1 = "Hello";var text2 = "World";var text3 = text1.concat(" ", text2); // returns Hello World
var str = "Hello world!";str.charAt(1); //returns e
charCodeAt() return the unicode of the character.
var str = "HELLO WORLD!";str.charCodeAt(0); //returns 72
split() convert a string into an array
<!DOCTYPE html><html><body><p id="demo"></p><script>var str = "Hello";var arr = str.split("");var text = "";var i;for (i = 0; i < arr.length; i++) { text += arr[i] + "<br>"}document.getElementById("demo").innerHTML = text;</script></body></html>
Reference:http://www.w3schools.com/js/js_strings.asp
0 0
- Common JS String Methods
- 3.7.6 - String Methods
- JS Array ECMAScript5 Methods
- Common HTTP Response Codes & HTTP Request Methods
- The ASP.NET common Development methods
- [Javascript String Math] String Methods, eval(), Math Methods
- common.js
- common.js
- common.js
- common.js
- Common.js
- common.js
- python string methods 和 string module区别
- JS.Class - 2. Methods & Types
- Vue.js computed vs Methods
- 常用js (common.js)
- String object's properties and methods
- String的常用pre-defined methods
- VMware安装centos,并配置网络服务
- 一颗小白菜的编程之旅起点
- JDK环境变量搭配
- 关于Jquery或者JavaScript获取不到DOM对象的一个原因
- [编译环境][gas]结构体定义
- Common JS String Methods
- FFMPEG支持的所有编码解码
- Linux_VMWare12 Install RHEL7
- Utilize Avahi Libraries in Android for mDNS Discovering
- 你真的了解word-wrap和word-break的区别吗?
- (转) External Sort
- STM32的内核CM3没有理解,NVIC,EXTI都涉及到了,内核到目前为止没有理解
- Dalvik和ART
- Android 的 SQLLite数据库