kenlistian

勤学多思

  IT博客 :: 首页 :: 新随笔 ::  :: 聚合  :: 管理 ::
  412 随笔 :: 0 文章 :: 23 评论 :: 0 Trackbacks



as3中字符串的处理

1.length
2.charAt ,charCodeAt
3.比较字符串
4.toString
5.+ concat
6. toLowerCase() or toUpperCase()
7. 字符串查找
8  隔离字符串
9  查找,替换,匹配字符串。


length 属性   每个字符串都有 length 属性,其值等于字符串中的字符数:

var str:String = "Adobe";
trace(str.length);  



使用 charAt() 方法和 charCodeAt() 方法检查字符串各个位置上的字符:

var str:String = "hello world!";
for (var:i = 0; i < str.length; i++)
{
    trace(str.charAt(i), "-", str.charCodeAt(i));
}


比较字符串   <、<=、!=、==、=> 和 >。

获取其它对象的字符串表示形式,以toString() 方法来实现此目的。
var n:Number = 99.47;
var str:String = n.toString();
    // str == "99.47"


字符串连接的含义是:将两个字符串按顺序合并为一个字符串。
  用 + 运算符来连接两个字符串
  和 concat() 方法


在大小写之间转换字符串
 toLowerCase() 方法和 toUpperCase() 方法分别将字符串中的英文字母字符转换为小写和大写:


substr() 和 substring() 返回字符串的一个子字符串。

在这两个方法中,第一个参数是给定字符串中起始字符的位置。
在 substr() 方法中,第二个参数是要返回的子字符串的"长度",
在 substring() 方法中,第二个参数是子字符串的"结尾"处字符的位置(该字符不包含在返回的字符串中)。
如:
var str:String = "Hello from Paris, Texas!!!";
trace(str.substr(11,15)); // 输出:Paris, Texas!!!
trace(str.substring(11,15)); // 输出:Pari

slice() 方法的功能类似于 substring() 方法。但可以使用负整数作为参数,
此时字符位置将从字符串末尾开始向前算起,

var str:String = "Hello from Paris, Texas!!!";
trace(str.slice(11,15)); // 输出:Pari
trace(str.slice(-3,-1)); // 输出:!!
trace(str.slice(-3,26)); // 输出:!!!
trace(str.slice(-3,str.length)); // 输出:!!!
trace(str.slice(-8,-3)); // 输出:Texas

查找匹配子字符串的字符位置
indexOf() 和 lastIndexOf()在字符串内查找匹配的子字符串,

var str:String = "The moon, the stars, the sea, the land";
trace(str.indexOf("the")); //输出:10

var str:String = "The moon, the stars, the sea, the land"
trace(str.indexOf("the", 11)); // 输出:21

lastIndexOf() 方法在字符串中查找子字符串的最后一个匹配项:

var str:String = "The moon, the stars, the sea, the land"
trace(str.lastIndexOf("the")); // 输出:30

如为 lastIndexOf() 方法提供了第二个参数,搜索将从字符串中的该索引位置反向(从右到左)进行:

var str:String = "The moon, the stars, the sea, the land"
trace(str.lastIndexOf("the", 29)); // 输出:21


创建由分隔符分隔的子字符串数组
split() 方法创建子字符串数组,该数组根据分隔符进行划分。

var queryStr:String = "first=joe&last=cheng&title=manager&StartDate=3/6/65";
var params:Array = queryStr.split("&", 2); // params == ["first=joe","last=cheng"]

split() 方法的第二个参数是可选参数,该参数定义所返回数组的最大大小。

另 split还支持正则表达式作为分隔符处理,这里不涉及正则处理

在字符串中查找模式并替换子字符串
 match() 和 search() 方法可查找与模式相匹配的子字符串。
 replace() 方法可查找与模式相匹配的子字符串并使用指定子字符串替换它们。
 search() 方法返回与给定模式相匹配的第一个子字符串的索引位置。


var str:String = "The more the merrier.";
trace(str.search("the")); // 输出:9

对于search,match,replace都支持正则表达式匹配处理。




posted on 2008-06-05 13:37 kenlistian 阅读(7088) 评论(0)  编辑 收藏 引用 所属分类: Flash
只有注册用户登录后才能发表评论。