字符串处理
字符串处理
<!DOCTYPE html> <html> <head> <title>字符串处理</title> </head> <body> <h1>字符串处理</h1> 原始字符串: <span id="originalString"></span><br/> trim 去除首尾空格:<span id="trimString"></span><br/> indexOf("ho") ho首次出现的位置:<span id="indexOfString"></span><br/> split(" ") 把一个字符串分割成字符串数组:<span id="splitString"></span><br/> replace(" ","-") 把首个空格替换为-:<span id="replaceString"></span><br/> replaceAll(" ","-") 把所有空格替换为-:<span id="replaceAllString"></span><br/> substr(1,3) 索引1-3的值包括1-3 <span id="substrString"></span><br/> substring(1,3) 索引1-3的值包括1-2 不包括3 <span id="subString"></span><br/> toLowerCase 变更为小写 <span id="toLowerCaseString"></span><br/> toUpperCase 变更为大写 <span id="toUpperCaseString"></span><br /> <h1>数据比对</h1> "3"==3?<span id="spnEquals"></span><br /> "3"===3?<span id="spnFullEquals"></span><br /> "4"!=4?<span id="spnNotEquals"></span><br /> "4"!==4?<span id="spnFullNotEquals"></span><br /> <script type="text/javascript"> var originalString = " Welcome to our hotel "; var trimString = originalString.trim(); var indexOfString = originalString.indexOf("ho"); var splitString = originalString.split(" "); var replaceString = originalString.replace(" ","-"); var replaceAllString = originalString.replaceAll(" ","-"); var substrString = originalString.substr(1,3); var subString = originalString.substring(1,3); var toLowerCaseString = originalString.toLowerCase(); var toUpperCaseString = originalString.toUpperCase(); document.getElementById("originalString").innerText=originalString + "(字符长度:"+originalString.length+")"; document.getElementById("trimString").innerText=trimString + "(字符长度:"+trimString.length+")"; document.getElementById("indexOfString").innerText=indexOfString; document.getElementById("splitString").innerText=splitString; document.getElementById("replaceString").innerText=replaceString; document.getElementById("replaceAllString").innerText=replaceAllString; document.getElementById("substrString").innerText=substrString; document.getElementById("subString").innerText=subString; document.getElementById("toLowerCaseString").innerText = toLowerCaseString; document.getElementById("toUpperCaseString").innerText = toUpperCaseString; document.getElementById("spnEquals").innerText = ("3"==3); document.getElementById("spnFullEquals").innerText = ("3"===3); document.getElementById("spnNotEquals").innerText = ("4"!=4); document.getElementById("spnFullNotEquals").innerText = ("4"!==4); </script> </body> </html>
原始字符串:
trim 去除首尾空格:
indexOf("ho") ho首次出现的位置:
split(" ") 把一个字符串分割成字符串数组:
replace(" ","-") 把首个空格替换为-:
replaceAll(" ","-") 把所有空格替换为-:
substr(1,3) 索引1-3的值包括1-3
substring(1,3) 索引1-3的值包括1-2 不包括3
toLowerCase 变更为小写
toUpperCase 变更为大写
数据比对
"3"==3?"3"===3?
"4"!=4?
"4"!==4? 标签:originalString,处理,javaScript,var,getElementById,innerText,字符串,document From: https://www.cnblogs.com/dwdw/p/16729364.html