Home  >  Article  >  Web Front-end  >  A brief discussion on string object processing encountered in javascript

A brief discussion on string object processing encountered in javascript

高洛峰
高洛峰Original
2016-12-06 14:40:391402browse

Parameter processing in javascript:

<script>
function getParam()
{
urlInfo=window.location.href; //获取当前页面的url
intLen=urlInfo.length; //获取url的长度
offset=urlInfo.indexOf("?"); //设置参数字符串开始的位置
strKeyValue=urlinfo.substr(offset,len); //取出参数字符串 这里会获得类似“id=1”这样的字符串
arrParam=strKeyValue.split("="); //对获得的参数字符串按照“=”进行分割
strParamValue=arrParam[1]; //得到参数值
alert("您要传递的参数值是"+strParamValue);
}
</script>

Properties of the String object in javascript:

length--returns the length of the string, not a function, no parentheses are required.

prototype--Add properties and methods

There are some functions for handling String objects in JavaScript:

concat() - Combine two or more characters of text and return a new string. (Basically, they are spliced ​​together by themselves)

charAt(a) - Returns the character a at the specified position. (It is rarely used, but I feel it is quite useful)

indexOf(a) - Returns the index of the first occurrence of a substring a in the string. If there is no match, it returns -1. (Frequently used)

lastIndexOf (a) - Returns the index of the last occurrence of a substring a in the string, or -1 if there is no match. (Frequently used)

match(reg) - Checks whether a string matches a regular expression reg. (Regular expressions have always been weak, take the time to learn them carefully)

substring(begin,end) - Returns a substring of the string, and the parameters include both ends. (Note that all are lowercase)

substr(start,length) - Returns the specified number of characters extracted from the string starting from the start subscript. (Do not confuse with the above one)

split(separator,howmany) - separator is a string or regular expression (required), howmany specifies the length of the returned array (optional) --- commonly used

replace(regexp/substr , replacement) - Replace characters (regularity is important)

toLowerCase() - Convert the entire string to lowercase letters.

toUpperCase() – Convert the entire string to uppercase letters.


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn