Javascript学习笔录10(字符串的操作)

25 篇文章 0 订阅
23 篇文章 0 订阅

1 字符串字体加粗,字体大写

string.bold()

string.toUpperCase()

2截取字段

  String.substr(N1,N2) 这个就是我们常用的从指定的位置(N1)截取指定长度(N2)的字符串; 
   String.substring(N1,N2) 这个就是我们常用的从指定的位置(N1)到指定的位置之前(N2)的字符串;  

  个数从0开始 
  举个例子:
  document.write(str1.substring(0,13)+"<br>") //fdsf1111 gfdg
  document.write(str1.substr (20,11)+"<br>")  // f cccc dddd

3 链接字符串

  str1.concat

  str1+=str2

4 格式化字符串

   字符串.格式化方式()

   document.write(str1.italics()) //斜体

5 创建锚

    str1="begin ,update this node";
    str2="link to anchor";
    document.write(str1. anchor("anc"));

    document.write(str2.link("#anc"))

6 搜索字符串

    str1.search("end")

7 定位字符串中的字符

   strObj.indexOf(subString[, startIndex])

返回 String 对象内第一次出现子字符串的字符位置

document.write("the index for the second word ‘and' is:"+str1.indexOf("and",30)+"</br>")//从第30个字符之后,出现and字符位置

  strObj.lastIndexOf(substring[, startindex])
 返回 String 对象中子字符串最后出现的位置。
 参数: startindex  可选项。

 该整数值指出在 String 对象内进行查找的开始索引位置。如果省略,则查找从字符串的末尾开始。
  如果没有找到子字符串,则返回 -1。
  如果 startindex 是负数,则 startindex 被当作零。如果它比最大字符位置索引还大,则它被当作最大的可能索引。
 从右向左执行查找。否则,该方法和 indexOf 相同。

8 字符串替换

 stringObject.replace(regexp/substr,replacement)

参数: regexp 具有全局标志 g,那么 replace() 方法将替换所有匹配的子串。否则,它只替换第一个匹配子串。

document .write(str1.replace("and",","))//替换第一个and =>,

具体代码:


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Js8.aspx.cs" Inherits="Javascript_Js8" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
    </div>
    </form>
</body>
</html>
<script>
myString="dsjfsdfjsdfsdfhsfiuhfihehe";
document.write(myString+"</br>");
document.write(myString.bold()+"</br>");
document.write(myString.toUpperCase()+"</br>");
str1="fdsf1111 gfdgfd dfdsf cccc dddd.<br>"
document.write(str1)
document.write(str1.substring(0,13)+"<br>") //fdsf 1111 gf  fdsf1111 gfdg
document.write(str1.substr (20,11)+"<br>")  //dsf cccc dd   f cccc dddd
alert("123456789".substr(2,5)) 
alert("123456789".substring(2,5)) 
str1="I Like";
str2="basketball and football and so on ";
document.write(str1+"</br>")
document.write(str2+"</br>")
document.write(str1.concat(str2)+"</br>")
document.write(str1+=str2)
str1="food and meat</br>"
document.write(str1)
document.write(str1.italics())
document.write(str1.strike())
str1="this is the bigginning of the page.<br>"
str2="….<br>"
str3="this is the end of the page .<br>"
str4="link to the start<br>"
str5="link to the end<br>"
str6="Link to the anchor1"
document.write(str1.anchor("start"))//输出str1,同时,给str1创建一个名叫start的锚,锚的值this is the bigginning of the page.
for(i=0;i<100;i++)
document.write(str2);
document.write(str3.anchor("end"))
document.write(str4.link("#start"))
document.write(str5.link("#end"))//str5 格式化为link,并绑定锚 http://localhost:25655/jQuery/Javascript/Js8.aspx#end
str1="this is the end of the line.<br>"
document.write(str1)
document.write("字符end在字符串的位置是"+str1.search("end")+"</br>")//index=0
document.write("字符dog在字符串的位置是"+str1.search("dog")+"</br>")
str1="spring is a time for flowers and trees and much hope and happy<br>"
document.write(str1+"</br>")
document.write("the index for the second word ‘and' is:"+str1.indexOf("and",30)+"</br>")
document.write("the last index of the word ‘and' is "+str1.lastIndexOf("and")+"</br>")
str1="spring is a time for flowers and trees and baby bunnles<br>"
document.write(str1)
document .write(str1.replace("and",","))
str1="spring is a time for flowers and trees and much hope and happy<br>"
document.write(str1)
str1array=str1.split(" ")
document.write(str1array[0]+"<br>")
document.write(str1array[1]+"<br>")
document.write(str1array[2]+"<br>")
document.write(str1array[3]+"<br>")

</script>



 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值