1. Replacing substrings
Replace (search,replacement)
Find the substring that satisfies the criteria search, replace with replacement
Note: By default, only the first substring found is replaced, and if you want to replace all, you need to write:
x.replace (/a/g, o var regs = new RegExp ( a ", " g "; X.replace (regs, " o );
<script>
function P (s) {
document.write (s);
document.write ("<br>");
}
var x = new String ("Hello JavaScript");
P (' This is the original string: ' +x ');
var y = x.replace ("A", "O");
P (' Replace only the first A: ' +y);
var regs = new RegExp ("A", "G");
var z = x.replace (regs, "O");
P (' Replace all A: ' +z);
</script>
2. Intercept a substring
SUBSTRING intercept a substring
Note: The second parameter, cannot be taken
Cases:
<
script
>
var x = new String("Hello JavaScript");
document.write( ‘字符串x的值: ‘+x);
document.write(‘<
br
>‘);
document.write(‘x.substring (0,3) 获取位0到3的字符串: ‘+x.substring (0,3) );
document.write(‘<
br
>‘);
document.write(‘左闭右开,取得到0,去不到3‘);
</
script
>
3. Returns the character at the specified position
CharAt returns the character at the specified position
charCodeAt returns the Unicode code corresponding to the character at the specified position
<
script
>
var y = new String("Hello JavaScrpt");
document.write("字符串y的值:"+y);
document.write("<
br
>");
document.write(‘通过 charAt(0)获取位置0的字符串: ‘+y.charAt(0)); //返回H
document.write("<
br
>");
document.write(‘通过 charCodeAt(0)获取位置0的字符的 Unicode码 :‘+y.charCodeAt(0)); //返回H对应的Unicode码 72
</
script
>
JavaScript string manipulation