JavaScript基础学习(一)--对象实例String的学习

JavaScript对象实例

String(字符串)对象操作:

//注意:id=“demo”的p标签和button标签是做测试用

<p id="demo">Click the button to locate where in the string a specifed value occurs.</p>
<button οnclick="myFunction()">Try it</button>

<script>
var txt = "Hello World!";

String字符串的属性如下:

//字符串的 长度--length
document.write(txt.length);

//允许向对象添加属性和方法

function employee(name,jobtitle,born)
{
this.name=name;
this.jobtitle=jobtitle;
this.born=born;
}
var fred=new employee("Fred Flintstone","Caveman",1970);
employee.prototype.salary=null;
fred.salary=20000;
document.write(fred.salary);
</script>


String的方法如下:

//字符串添加样式

<script>

document.write("<p>Big: " + txt.length + "</p>");
document.write("<p>Big: " + txt.big() + "</p>");//大号字体显示字符串
document.write("<p>Small: " + txt.small() + "</p>");
document.write("<p>Bold: " + txt.bold() + "</p>");
document.write("<p>Italic: " + txt.italics() + "</p>");
document.write("<p>Fixed: " + txt.fixed() + "</p>");//以打字机文本显示字符串
document.write("<p>Strike: " + txt.strike() + "</p>");//在字符串上画上横线
document.write("<p>Fontcolor: " + txt.fontcolor("green") + "</p>");
document.write("<p>Fontsize: " + txt.fontsize(6) + "</p>");
document.write("<p>Subscript: " + txt.sub() + "</p>");//把字符串显示为下标
document.write("<p>Superscript: " + txt.sup() + "</p>");//使用小字号来显示字符串
document.write("<p>Link: " + txt.link("http://www.w3cschool.cc") + "</p>");//将字符串显示为链接
document.write("<p>Blink: " + txt.blink() + " (does not work in IE, Chrome, or Safari)</p>");//显示闪动字符串
</script>

<script>

var str = "HELLO WORLD";
var n = str.charCodeAt(0);//返回72

var n = str.charAt(2);//返回L

var str1 = "Hello ";
var str2 = "world!";

var str3=" Have a nice day!";
var n = str1.concat(str2,str3);//连接两个或更多的字符串

var n = String.fromCharCode(72,69,76,76,79);//返回 HELLO  将Unicode编码转换为一个字符串

</script>


//返回字符串中中指定文本首次出现的位置 --indexOf()

function myFunction()
{
var str="Hello world, welcome to the universe.";
var n=str.indexOf("welcome");
document.getElementById("demo").innerHTML=n;//返回12  首次出现的位置


var str="Hello planet earth, you are a great planet.";
var n=str.lastIndexOf("planet");

var str="Hello planet earth, you are a great planet.";
var n=str.lastIndexOf("planet",20);//该方法是从未到头的检索 看它是否含有子串 searchvalue。开始检索的位置在字符串的 fromindex 处或字符串的结尾(没有指定 fromindex 时)。如果找到一个 searchvalue,则返回 searchvalue 的第一个字符在 stringObject 中的位置。stringObject 中的字符位置是从 0 开始的。

}


//查找字符串中指定的字符,若查找到返回该字符--match()

<script>
var str="Hello world!";
document.write(str.match("world") + "<br>");//返回为word
document.write(str.match("World") + "<br>");//返回为null
document.write(str.match("worlld") + "<br>");//返回为null
document.write(str.match("world!"));//返回为word!

var str="The rain in SPAIN stays mainly in the plain"; 
var n=str.match(/ain/g);//返回结果:ain,ain,ain

这个方法的行为在很大程度上有赖于 regexp 是否具有标志 g。如果 regexp 没有标志 g,那么 match() 方法就只能在 stringObject 中执行一次匹配。如果没有找到任何匹配的文本, match() 将返回 null


</script>

//替换字符串中的字符

<script>
function myFunction()
{
var str=document.getElementById("demo").innerHTML; 
var n=str.replace("Microsoft","W3CSchool");
document.getElementById("demo").innerHTML=n;
}
</script>


除此之外,对String的方法还有:

Split();substr()【可以替代substring()和slice()】,toLowerCase(),toUpperCase();trim();valuesOf()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值