如何在 Pharo 中对字符串进行驼峰式命名?
我试图从:
'hello how are you today'
到
'helloHowAreYouToday'
我认为 asCapitalizedPhrase asLegalSelector
可以解决问题,但事实并非如此。
执行此操作的正确方法是什么?
编辑:
我想我应该澄清我的问题;我已经有一种方法可以将字符串转换为驼峰式选择器:
|aString aCamelCaseString|
aString := aString findTokens: $ .
aCamelCaseString := aString first.
aString allButFirst do: [:each | aCamelCaseString := aCamelCaseString , each capitalized].
我只是想知道 Pharo 是否有一个标准的系统方法来实现相同的目的:)
I'm trying to get from:
'hello how are you today'
to
'helloHowAreYouToday'
And I thought asCapitalizedPhrase asLegalSelector
would do the trick, but it doesn't.
What's the proper way to do this?
EDIT:
I think I should clarify my question; I already have a way to transform a string into a camelCase selector:
|aString aCamelCaseString|
aString := aString findTokens: $ .
aCamelCaseString := aString first.
aString allButFirst do: [:each | aCamelCaseString := aCamelCaseString , each capitalized].
I was just wondering whether Pharo has a standard system method to achieve the same :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这个怎么样?
How about this?
我认为没有现有的方法可以做到这一点。
这是解决您的问题的一个实现:
编辑:注意,与 Frank 的解决方案相比,这个解决方案更长,但它不会因空输入而中断,并且不会为每个创建一个新的字符串实例 步骤,因为它流过输入,这更有效(如果您有大字符串)。
I don't think there's an existing method doing this.
Here's an implementation that solves your problem:
Edit: note, in comparison to Frank's solution this one is longer but it does not break for empty input and it does not create a new string instance for each step since it streams over the input, which is more efficient (in case you have large strings).
你没有说你正在使用哪个版本的 Pharo,但在稳定的 5.0 中,
会产生
To get what I'm using run:
You don't say which version of Pharo you're using, but in the stable 5.0,
yields
To get what I'm using run:
我知道这很旧,但 Squeak 有一个有用的实现(String>>asCamelCase),它基本上是这样做的:
I know this is old but Squeak has a useful implementation (String>>asCamelCase) which basically does this: