所以我有一个字符串“SmartUserWantsToLive”我想从它或任何这样的字符串生成大写字母字符串,如“智能用户想要生活”。我该怎么做?
答案 0 :(得分:5)
var str1:String = 'SmartUserWantsToLive';
var str2:String = str1.replace( /([A-Z])/g, ' $1' );
// split first character when it was a space, to support strings like 'fooBar'
if ( str2.charAt( 0 ) == ' ' )
str2 = str2.substr( 1 );
trace( str2 ); // 'Smart user Wants To Live'
编辑每条评论
var str3:String = 'SomeUsefulAPIFooBar';
var str4:String = str3.replace( /((?<![A-Z])[A-Z]|[A-Z](?![A-Z]))/g, ' $1' );
if ( str4.charAt( 0 ) == ' ' )
str4 = str4.substr( 1 );
trace( str4 ); // 'Some Useful API Foo Bar'