开发者

How to put spaces before Capital letters in actionscript?

开发者 https://www.devze.com 2022-12-23 18:55 出处:网络
So I have开发者_如何学C a string \"SmartUserWantsToLive\" I want to generate from it or any such string with capital letters strings like \"Smart User Wants To Live\". How do I do this?var str1:String

So I have开发者_如何学C a string "SmartUserWantsToLive" I want to generate from it or any such string with capital letters strings like "Smart User Wants To Live". How do I do this?


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'

edit: Per comment

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'
0

精彩评论

暂无评论...
验证码 换一张
取 消