1.调用String类的toLowerCase方法和toUpperCase方法大小写转换:
若不区分大小写的查找可以提前调用toLowerCase()方法做个大小写转换后查找
这里String.charAt()返回参数位置指定的字符。String.substr()返回子串。
package
{
import flash.display.Sprite;
/**
* 字符串大小写转换
* @author xcf007
*/
public class case1 extends Sprite
{
public function case1()
{
var example:String = "What case?";
trace(example.toLowerCase()); //转化为小写
trace(example.toUpperCase()); //转化为大写
trace(example.valueOf());
}
}
}
{
import flash.display.Sprite;
/**
* 字符串大小写转换
* @author xcf007
*/
public class case1 extends Sprite
{
public function case1()
{
var example:String = "What case?";
trace(example.toLowerCase()); //转化为小写
trace(example.toUpperCase()); //转化为大写
trace(example.valueOf());
}
}
}
2.不区分大小写的比较:
//不区分大小写的比较
var sTitleOne:String = new String( "ActionScript Bible");
var sTitleTwo:String = new String( "ActionScript bible");
trace(sTitleOne.valueOf() == sTitleTwo.valueOf()); //false
trace(sTitleOne.toUpperCase() == sTitleTwo.toUpperCase()); //true
var sTitleOne:String = new String( "ActionScript Bible");
var sTitleTwo:String = new String( "ActionScript bible");
trace(sTitleOne.valueOf() == sTitleTwo.valueOf()); //false
trace(sTitleOne.toUpperCase() == sTitleTwo.toUpperCase()); //true
3.lastIndexOf从右向左查找第一次匹配结果位置:
var example:String =
"Cool. This is a cool as both cool (lowercase) and Cool.";
var search:String = "cool";
trace(example.lastIndexOf(search)); //最后一次匹配的首字符索引位置
var search:String = "cool";
trace(example.lastIndexOf(search)); //最后一次匹配的首字符索引位置
trace( example.toLowerCase( ).lastIndexOf( search ) );
4.首字母大写形式:
public function case1()
{
var example:String = "bRuCE";
trace(toInitialCap(example)); // 首字母大写形式,Displays: Bruce
}
public static function toInitialCap( original:String ):String {
return original.charAt(0).toUpperCase() + original.substr(1).toLowerCase();
}
{
var example:String = "bRuCE";
trace(toInitialCap(example)); // 首字母大写形式,Displays: Bruce
}
public static function toInitialCap( original:String ):String {
return original.charAt(0).toUpperCase() + original.substr(1).toLowerCase();
}
本文转自 xcf007 51CTO博客,原文链接:http://blog.51cto.com/xcf007/161647,如需转载请自行联系原作者