1. Calculate the length of a string
Copy Code code as follows:
var txt= "Hello world!"
document.write (Txt.length)
2.indexOf () method
How to use IndexOf () to position the first occurrence of a specified character in a string.
Copy Code code as follows:
<script type= "Text/javascript" >
var str= "Hello world!"//w lowercase
document.write (Str.indexof ("H") + "<br/>")//0
document.write (Str.indexof ("World") + "<br/>")//-1
document.write (Str.indexof ("World"))//6
</script>
3.match () method
Finds a specific character in a string, and returns this character if it is found.
Copy Code code as follows:
<script type= "Text/javascript" >
var str= "Hello world!"
document.write (Str.match ("World") + "<br/>")//world
document.write (Str.match ("World") + "<br/>")//null
document.write (Str.match ("worlld") + "<br/>")//null
document.write (Str.match ("world!"))//world!
</script>
4. How to replace characters in a string-replace ()
Replace some characters in a string with the Replace () method.
Copy Code code as follows:
<script type= "Text/javascript" >
var str= "Visit cnblogs.com!"
document.write (Str.replace (/cnblogs.com/, "mamogu.com"))
</script>