javaScript内置对象String

String
<script>
var str=new String("a,b,c,d,e,f,g");
var str="a,b,c,d,e,f,g";
alert(str.length);
alert(str.substr(4));
</script>
第一句和第二句差不多,所以可以直接用第二句
str.length字符串长度
str.substr(4)取后4个长度

方法:
 

anchor();
用于创建html锚点

例:
<script>
var txt="Hello world!"
document.write(txt.anchor("myanchor"))
</script>

big();
字符串显示大号字体

例:
<script>
var str="Hello World!";
document.write(str.big());
</script>

blink();
显示闪动的文本

例:
<script>
var str="hhhh";
document.write("<p>blink:"+str.blink()+"</p>");
</script>

bold();
字符串显示粗体

例:
<script>
var str="hhhh";
document.write("<p>bold:"+str.bold()+"</p>");
</script>

charAt();
返回指定位置的字符,即字符在字符串中的下标

例:
<script>
var str="hcndsih";
document.write(str.charAt(2));
</script>
结果:n

charCodeAt();
返回指定位置的字符的unicode编码,返回的是0-65535之间的整数

例:
<script>
var str="hcndsih";
document.write(str.charCodeAt(2));
</script>
结果:110

concat();
用于连接两个或多个字符串,等同于+的功能

例:
<script>
var str1="Hello ";
var str2="World";
document.write(str1.concat(str2));
</script>
结果:Hello World

fixed();
把字符串显示打印机的字体

例:
<script>
var str1="Hello World";
document.write(str1.fixed());
</script>

fontcolor();
指定颜色显示字符串

例:
<script>
var str="Hello World!";
document.write(str.fontcolor("#900B09"));
</script>

fontsize();
按照指定的尺寸来显示字符串,参数是1-7的数字

例:
<script>
var str="Hello World!";
document.write(str.fontsize(7));
</script>

fromCharCode();
创建的字符串中的字符的Unicode编码,显示出字符

例:
<script>
document.write(String.fromCharCode(72,69,76,76,79));
document.write("<br />");
document.write(String.fromCharCode(65,66,67));
</script>
结果:HELLO ABC

indexOf();
检索字符串,返回指定字符串值在字符串中首次出现的位置,对大小写敏感,没检索到就返回-1

例:
<script>
var str="Hello World!";
document.write(str.indexOf("Hello")+"<br>");
document.write(str.indexOf("world")+"<br>");
document.write(str.indexOf("World"));
</script>
结果:0 -1 6

italics();
字符串显示斜体

例:
<script>
var str="Hello World!";
document.write(str.italics());
</script>

lastIndexOf();
从尾到头检索字符串,返回字符串值第一个字符的位置,对大小写敏感。没有检索到返回-1

例:
<script>
var str="Hello world!"
document.write(str.lastIndexOf("Hello") + "<br />")
document.write(str.lastIndexOf("World") + "<br />")
document.write(str.lastIndexOf("world"))
</script>

link();
字符串显示为超链接

例:
<script>
var str="Hello world!"
document.write(str.link(" http://baidu.com"));
</script>

localeCompare();
用指定的顺序比较两个字符串,如果第一个小于第二个,返回小于0的数,如果第一个大于第二个,返回大于0的数,字符串相等,返回0

例:
<script>
var str;
str.sort (function(a,b){return a.localeCompare(b)});
</script>
结果:a,b

match();
在字符串内检索指定的值,返回指定的值。没有检索到值就返回null

例:
<script>
var str="Hello world!"
document.write(str.match("world")+"<br>")
document.write(str.match("worlld")+"<br>")
document.write(str.match("world!")+"<br>")
</script>
结果:world null world!

replace();
用其中一些字符串替换另一些字符串,查找和替换操作

例:找到Microsoft替换成hhh
<script>
var str="Welcome to Microsoft!"
str=str+"We are proud to announce that Microsoft has"
str=str+"one of the largest Web Developers sites in the world."
document.write(str.replace(/Microsoft/g,"hhh"))
</script>
结果:Welcome to hhh!We are proud to announce that hhh has one of the largest Web Developers sites in the world.
小写替换成大写
<script>
var str="javascript Tutorial";
document.write(str.replace(/javascript/i,"JavaScript"));
</script>
结果:JavaScript Tutorial
把Doe, John转换成John,Doe
<script>
var str="Doe,John";
document.write(str.replace(/(\w+)\s*,\s*(\w+)/,"$2 $1"));
</script>
结果:John,Doe

把双引号改成单引号
<script>
var str='"a","b"';
document.write(str.replace(/"([^"]*)"/g,"'$1'"));
</script>
结果:'a','b'

把首字母变成大写
<script>
var str='aaa bbb ccc';
document.write(str.replace(/\b\w+\b/g,function(hh){return hh.substring(0,1).toUpperCase()+hh.substring(1);}));
</script>
结果:Aaa Bbb Ccc

search();
检索字符串中指定的字符串,返回的结果如果有匹配的就返回相匹配字符串的起始位置,如果没有检索到,就返回-1,默认检索分大小写

例:
<script type="text/javascript">
var str="Visit W3School!"
document.write(str.search(/w3school/i))
</script>
结果:6

slice();
提取字符串的某个部分,以新的字符串。返回被提取的部分,包括开始,不包括结束,-1指的最后一个字符,以此类推

例:
<script>
var str="Hello happy world!";
document.write(str.slice(6,14));
</script>
结果:happy wo

small();
字符串显示小号字

例:
<script>
var str="Hello World!";
document.write(str.small());
</script>

split();
把字符串分割成字符串数组

例:
<script>
var str="How are you doing today?";
document.write(str.split(" ")+"<br>");
document.write(str.split("")+"<br>");
document.write(str.split(" ",4)+"<br>");
document.write(str.split("",5));
</script>
结果:How,are,you,doing,today? H,o,w, ,a,r,e, ,y,o,u, ,d,o,i,n,g, ,t,o,d,a,y,? How,are,you,doing H,o,w, ,a
特殊符号分割
<script>
var str="2|3|4|5|6";
document.write(str.split("|"));
</script>
结果:2,3,4,5,6
句子分割成单词:
<script>
var str="How are you doing now?";
document.write(str.split(/\s+/));
</script>
结果:How,are,you,doing,today?

strike();
字符串显示删除线

例:
<script>
var str="How are you doing now?";
document.write(str.strike());
</script>

sub();
字符串显示下标

例:
<script>
var str="How are you doing now?";
var hhh="啦啦";
document.write(str+hhh.sub());
</script>

substr();
从下标开始提取指定数目的字符,-1表示最后一个字符以此类推。

例:
<script>
var str="How are you doing now?";
document.write(str.substr(3,7));
</script>
结果:are yo

substring();
用于提取字符串中介于指定下标之间的字符

例:
<script>
var str="How are you doing now?";
document.write(str.substring(3,10));
</script>
结果:are yo

sup();
字符串显示为上标

例:
<script>
var str="Hello World!";
document.write(str.sup());
</script>

toLocaleLowerCase();
把字符串转换成小写

例:
<script>
var str="HELLO WORLD";
document.write(str.toLocaleLowerCase());
</script>
结果:hello world

toLocaleUpperCase();
把字符串转化成大写

例:
<script>
var str="hello world";
document.write(str.toLocaleUpperCase());
</script>
结果:HELLO WORLD

toSource();
对象的源代码


toString();
返回字符串类型

例:
<script>
  var str = false.toString();
 document.write(str+"<br>", typeof str);
</script>
结果: false string

valueOf();
返回string对象原始值

猜你喜欢

转载自blog.csdn.net/abenazhan/article/details/77116094