<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>JavaScript string对象</title> </head> <body> <script> var name = new String('devZhang'); var job = "devZhang is an iOS developer !"; document.write("Two ways to create string objects: <br> 1, " + name + "<br> 2, " + job + "<br>"); var nameLength = name.length document.write("<br>The length of the string " + name + " is: " + nameLength + "<br>"); var indexChar = name.indexOf ('Z') document.write("<br> The position of the character 'Z' in the string " + name + ": " + indexChar + "<br>"); var charIndex = name.charAt (1) document.write("<br>string" + name + "The character at position 1 is: " + charIndex + "<br>"); var namejob = name.concat(" is an ").concat(job) document.write("<br>字符串拼接:" + namejob + "<br>") var nameUpper = name.toUpperCase() document.write("<br>The string is converted to uppercase: " + nameUpper + "<br>") var nameLower = name.toLowerCase() document.write("<br>The string is converted to lowercase: " + nameLower + "<br>") var jobTrim = job.trim () document.write("<br>Remove spaces on both sides of the string: " + jobTrim + "<br>") var jobReplace = job.replace("developer", "Developer") document.write("<br>字符串替换:" + jobReplace + "<br>") var jobSearch = job.search("eve") document.write("<br>string search substring position: " + jobSearch + "<br>") var jobSlice = job.slice (3.7) document.write("<br>Get the substring at the specified start and end position in the string: " + jobSlice + "<br>") var jobSplit = job.split(" ") document.write("<br>The substring is split according to the specified format string: " + jobSplit + "<br>") var jobSub = job.substr(1,3) document.write("<br>Get the substring of the specified position and length in the string: " + jobSub + "<br>") </script> </body> </html>
There are two ways to create string objects: 1、devZhang 2、devZhang is an iOS developer ! The length of the string devZhang is: 8 Position of character 'Z' in string devZhang: 3 The character at position 1 in the string devZhang is: e String concatenation: devZhang is an devZhang is an iOS developer ! Convert string to uppercase: DEVZHANG Convert string to lowercase: devzhang Remove the spaces on both sides of the string: devZhang is an iOS developer ! String replacement: devZhang is an iOS Developer ! String lookup substring position: 20 Get the substring at the specified start and end position in a string: Zhan The substring is split according to the specified format string: devZhang,is,an,iOS,developer,! Get a substring at a specified position and length in a string: evZ