<!doctype html> <html> <head> <meta charset="utf-8"> <title>截取,拼接字符串</title> <script type="text/javascript"> var str = "abc八dccef维jaagk研sew修wwq学rrt院mn"; /* 1.挨个从str中获取 八维研修学院 这几个字符,将获取的单个字符再拼接成完整的字符串 */ document.write("1. 挨个 截取字符 :"+"<br/><br/>"); var bwie1 = document.write(str. substring (3,4)+" "); var bwie2 = document.write(str. substring (9,10)+" "); var bwie3 = document.write(str. substring (15,16)+" "); var bwie4 = document.write(str. substring (19,20)+" "); var bwie5 = document.write(str. substring (23,24)+" "); var bwie6 = document.write(str. substring (27,28)); var n = document.write("<br/><br/><br/>"+"2. 拼接字符串 :<br/><br/>"); var newStr = document.write(str.substring(3,4). concat ( str.substring(9,10), str.substring(15,16), str.substring(19,20), str.substring(23,24), str.substring(27,28)) );
/* 2.使用字符串替换 replace() 的方式获取 八维研修学院 字符串 */ var r = document.write("<br/><br/><br/>"+"3. 替换字符串 :<br/><br/>"); var new_replace = document.write( str. replace (str,"八维研修学院")); </script> </head> <body> </body> </html>
Atas ialah kandungan terperinci 截取,拼接,替换字符的方法. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!