javascript 字符串处理全攻略
文章分类:
JavaScript
文章标题:
javascript 字符串处理全攻略
关 键 字:
0
文章作者:
alonglee
文章来源:
http://lmgq.vip.sina.com/tech/jsadvancedlesson/c2p1.htm
发表时间:
2004-9-21 14:43:00
一、声明字符串:
var normal_monkey = "I am a monkey!<br>";
document.writeln("Normal monkey " + normal_monkey);
var bold_monkey = normal_monkey.bold();
document.writeln("Bold monkey " + bold_monkey);
这里的声明: var bold_monkey = normal_monkey.bold();
和下面对声明是等同的:
var bold_monkey = "<b>" + normal_monkey + "</b>";
第1个版本的声明看起来要简明得多。这里用到了字符串对象中
的bold对象,其他的字符串对象还有indexOf, charAt,
substring, 以及split, 这些方法可以深入字符串的组成结构。
首先我们研究一下indexOf。
2、indexOf
indexOf用于发现一系列的字符在一个字符串中等位置并告诉你子字符串的起始位置。如
果一个字符串中部包含该子字符串则indexOf返回returns "-1."
例子:
var the_word = "monkey";
//让我们从单词 "monkey"开始。
var location_of_m = the_word.indexOf("m");
//location_of_m(字母m的位置)将为0,因为字母m位于该字符串的起始位置。
var location_of_o = the_word.indexOf("o");
//location_of_o(字母o的位置)将为1。
var location_of_key = the_word.indexOf("key");
//location_of_key(key的位置)将为3因为子字符串“key”以字母k开始,而k
在单词monkey中的位置是3。
var location_of_y = the_word.indexOf("y");
//location_of_y)字母y的位置)是5。
var cheeky = the_word.indexOf("q");
//cheeky值是-1,因为在单词“monk
相关文档:
Js学习(三)
另外一种方式通过直接实例化匿名函数方式来创建带有私有静态成员的类的例子跟上面的例子很相似:
new function() {
// private static fields
var s_first = 1;
var s_second = 2;
// private static methods
&n ......
利用调用继承的关键只有一步操作:
就是在子类定义时,通过父类的 call 方法,将子类的 this 指针传入。使父类方法在子类上下文中执行。
这样,父类中的所有在父类内部通过 this 方式定义的公有实例成员都会被子类继承。
用 instanceof 运算符判断时,子类的实例化对象只属于子类,不属于父类。
查看子类的实例化对象的 ......
<!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>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>js除去字符串空格 ......
有许多小窍门来使编程更加容易。其中之一就是eval()函数,这个函数可以把一个字符串当作一个JavaScript表达式一样去执行它。以下是它的说明
Eval 函数
功能:先解释Javascript代码,然后在执行它
用法:Eval(codeString)
codeString是包含有Javascript语句的字符串,在eval之后使用Javascript引擎编译。
举个小例子:
......
Java代码 import flash.external.ExternalInterface; function hello(){ return "测试成功了哦~~"; } //允许flash调用js函数 参数1:js函数名称 参数2:向js函数传递的参数 ExternalInterface.call("hello", "jacky"); ......