纳速健身
标题:
Flash截取同样长度的中英文字符串
[打印本页]
作者:
awagink
时间:
2008-4-9 16:24
标题:
Flash截取同样长度的中英文字符串
在ASP里面做过这种截取函数,今天在Flash也要用到。
就把ASP的代码拿到Flash改编一下。
代码如下:
============================================================
function
getStrValue(Str1:
String
, Len:
Number
, Str2:
String
):
String
{
var
n:
Number
= 0;
var
k:
Number
= 0;
for
(
var
i:
Number
= 0; n<Len; i++) {
if
(Str1.
charCodeAt
(i)<0 || Str1.
charCodeAt
(i)>255) {
n += 2;
if
(n>Len) {
k--;
}
}
else
{
n++;
}
k++;
}
var
Str3 = Str1.
substring
(0, k);
if
(Str3 == Str1) {
return
Str1;
}
else
{
return
Str3+Str2;
}
}
============================================================
使用方法:
public
getStrValue(String1:
String
,Length:
Number
,String2:
String
) :
String
String1,要处理的字符串;
Length,要保留字符串的长度(一个中文算两个字符长度);
String2,保留字符串后面显示的字符;
返回:字符串
实例:
============================================================
var
myString:
String
= "
Lorsxin 乱世狂闪 juyazhai.com
";
var
newString:
String
;
newString = getStrValue(myString, 0, "
...
");
trace
(newString);
//...
newString = getStrValue(myString, 1, "
...
");
trace
(newString);
//L...
newString = getStrValue(myString, 7, "
...
");
trace
(newString);
//Lorsxin...
newString = getStrValue(myString, 9, "
...
");
trace
(newString);
//Lorsxin ...
newString = getStrValue(myString, 10, "
...
");
trace
(newString);
//Lorsxin 乱...
newString = getStrValue(myString, 14, "
...
");
trace
(newString);
//Lorsxin 乱世狂...
newString = getStrValue(myString, 16, "
...
");
trace
(newString);
//Lorsxin 乱世狂闪...
newString = getStrValue(myString, 19, "
...
");
trace
(newString);
//Lorsxin 乱世狂闪 ju...
newString = getStrValue(myString, 29, "
...
");
trace
(newString);
//Lorsxin 乱世狂闪 juyazhai.com
newString = getStrValue(myString, 30, "
...
");
trace
(newString);
//Lorsxin 乱世狂闪 juyazhai.com
============================================================
欢迎光临 纳速健身 (https://nasue.com/)
Powered by Discuz! X3.4