Javascript学习笔录10(字符串的操作)
来源:互联网 发布:ubuntu的使用教程 编辑:程序博客网 时间:2024/06/05 07:49
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 gfdgdocument.write(str1.substr (20,11)+"<br>") //dsf cccc dd f cccc ddddalert("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#endstr1="this is the end of the line.<br>"document.write(str1)document.write("字符end在字符串的位置是"+str1.search("end")+"</br>")//index=0document.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>
- Javascript学习笔录10(字符串的操作)
- Javascript学习笔录11(日期的操作)
- javascript学习笔录1(【问题】JavaScript的相关问题)
- javascript学习笔录6(Javascript元素的常用属性)
- Javascript学习笔录16(锚点的应用)
- Javascript学习笔录17(Js调试的方法)
- Javascript学习笔录12(Math用法)
- Javascript学习笔录14(表单应用)
- Javascript学习笔录9(窗口属性操作-status、背景色、title等)
- Javascript学习笔录3(javascript的数组等一些体会)
- Javascript学习笔录4(Javascript 定时器,ifelse等)
- Javascript学习笔录5(Javascript 函数怎么玩)
- Javascript学习笔录8(学会image的应用和link的属性)
- Javascript学习笔录2(css基础概念)
- Javascript学习笔录13(标签用法总结)
- Javascript学习笔录15(JS navigator 对象,cookies)
- jQuery学习笔录7(jQuery中的文本操作)
- jQuery学习笔录8(jQuery中的表单操作)
- 深入 泛型
- C语言当中位段的使用
- 因果图方法
- 四个主流的Native-XML数据库
- 个人版机房收费系统总结
- Javascript学习笔录10(字符串的操作)
- onAttachedToWindow () 和 onDetachedFromWindow ()
- 实践 cisco c3560 恢复密码实例
- 文件操作小结
- ORA-01157 ORA-01110 Alter system check datafiles
- 常用系统下载地址
- C语言和设计模式(建造者模式) 【文章都是来自网络,只要自己吸取了就是自己的】
- c++栈的实现
- linux下安装VMware