如何在动作脚本中的大写字母之前放置空格?

时间:2010-03-20 00:31:22

标签: flash actionscript-3 string

所以我有一个字符串“SmartUserWantsToLive”我想从它或任何这样的字符串生成大写字母字符串,如“智能用户想要生活”。我该怎么做?

1 个答案:

答案 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'