All articles| All Pictures| All Softwares| All Video| Go home page| Write articles| Upload pictures

Reading number is top 10 articles
分析一下网页子窗口和网页父窗口的交互_JavaScript技术_编程技术
操作数据:中级SQL_mssql学习_编程技术
通过内存配置来优化SQL Server的性能_[SQL Server教程]
ASP.NET中多国语言的实现_[Asp.Net教程]
Ajax实例教程-----级联菜单_[AJAX教程]
PHP+MySQL分页显示示例分析_[PHP教程]
网友原创-从N层到.NET详细剖析原理_.net资料_编程技术
PHP检查表单提交是否来自于本站_[PHP教程]
无废话C#设计模式之三:Abstract,Factory_.net资料_编程技术
VS2005+Crystal,Report开发Web应用_[Asp.Net教程]
Reading number is top 10 pictures
Most cow mistress ZhaoGongXia face exposure
The real super beauty11
小学生考试又现神作--还有外国的
The real super beauty6
Magnificent cloud2
BingBingFan apple dew point photo gallery4
Wear bikinis cock
西游日记2
传几朵花
七步解决性冷淡
Download software ranking
asp.netWeb服务器高级编程
双旗镇刀客B
Unix video tutorial11
Call Of Duty2
Tram sex maniac 2 (H) rar bag5
好色的外科大夫
Unix video tutorial2
Kung fu panda - the secret of the teacher
I for your crazy
The Bermuda triangle1
delv published in(发表于) 2013/12/31 6:56:59 Edit(编辑)
脚本实例:用,javascript,来操作字符串_JavaScript技术_编程技术

脚本实例:用,javascript,来操作字符串_JavaScript技术_编程技术

脚本实例:用 javascript 来操作字符串_JavaScript技术_编程技术-你的首页-uuhomepage.com

  虽然 javascript 有很多用处,但是处理字符串是其中最流行的一个。下面让我们深入地分析一下使用 javascript 操作字符串。在 javascript 中, String 是对象。 String 对象并不是以字符数组的方式存储的,所以我们必须使用内建函数来操纵它们的值。这些内建函数提供了不同的方法来访问字符串变量的内容。下面我们详细看一下这些函数。


  包罗万象


  操作字符串的值是一般的开发人员必须面临的家常便饭。操作字符串的具体方式有很多,比如说从一个字符串是提取出一部分内容来,或者确定一个字符串是否包含一个特定的字符。下面的 javascript 函数为开发人员提供了他们所需要的所有功能:


  • concat() – 将两个或多个字符的文本组合起来,返回一个新的字符串。


  • indexOf() – 返回字符串中一个子串第一处出现的索引。如果没有匹配项,返回 -1 。


  • charAT() – 返回指定位置的字符。


  • lastIndexOf() – 返回字符串中一个子串最后一处出现的索引,如果没有匹配项,返回 -1 。


  • match() – 检查一个字符串是否匹配一个正则表达式。


  • substring() – 返回字符串的一个子串。传入参数是起始位置和结束位置。


  • replace() – 用来查找匹配一个正则表达式的字符串,然后使用新字符串代替匹配的字符串。


  • search() – 执行一个正则表达式匹配查找。如果查找成功,返回字符串中匹配的索引值。否则返回 -1 。


  • slice() – 提取字符串的一部分,并返回一个新字符串。


  • split() – 通过将字符串划分成子串,将一个字符串做成一个字符串数组。


  • length() – 返回字符串的长度,所谓字符串的长度是指其包含的字符的个数。


  • toLowerCase() – 将整个字符串转成小写字母。


  • toUpperCase() – 将整个字符串转成大写字母。


  注意: concat 、 match 、 replace 和 search 函数是在 javascript 1.2 中加入的。所有其它函数在 javascript 1.0 就已经提供了。


  下面让我们看一下如何在 javascript 使用这些函数。下面的代码是用到了前面提到的所有函数:


function manipulateString(passedString1, passedString2) {
var concatString;
// The string passed to concat is added to the end of the first string
concatString = passedString1.concat(passedString2);
alert(concatString);
// The following if statement will be true since first word is Tony
if (concatString.charAt(3) == "y") {
alert("Character found!"); }
// The last position of the letter n is 10
alert("The last index of n is: " + concatString.lastIndexOf("n"));
// A regular expression is used to locate and replace the substring
var newString = concatString.replace(/Tony/gi,"General");
// The following yields Please salute General Patton
alert("Please salute " + newString);
// The match function returns an array containing all matches found
matchArray = concatString.match(/Tony/gi);
for (var i=0; i
alert("Match found: " + matchArray[i]);
}
// Determine if the regular expression is found, a –1 indicates no
if (newString.search(/Tony/) == -1) {
alert("String not found");
} else {
alert("String found.");
}
// Extract a portion of the string and store it in a new variable
var sliceString = newString.slice(newString.indexOf("l")+2,newString.length);
alert(sliceString);
// The split function creates a new array containing each value separated by a space
stringArray = concatString.split(" ");
for (var i=0; i
alert(stringArray[i];
}
alert(newString.toUpperCase());
alert(newString.toLowerCase());
}





添加到del.icio.us 添加到新浪ViVi 添加到百度搜藏 添加到POCO网摘 添加到天天网摘365Key 添加到和讯网摘 添加到天极网摘 添加到黑米书签 添加到QQ书签 添加到雅虎收藏 添加到奇客发现 diigo it 添加到饭否 添加到飞豆订阅 添加到抓虾收藏 添加到鲜果订阅 digg it 貼到funP 添加到有道阅读 Live Favorites 添加到Newsvine 打印本页 用Email发送本页 在Facebook上分享


Disclaimer Privacy Policy About us Site Map

If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.