如何在 Pharo 中对字符串进行驼峰式命名?

发布于 2024-10-18 15:50:56 字数 555 浏览 7 评论 0原文

我试图从:

'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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

欢烬 2024-10-25 15:50:56

这个怎么样?

| tokens |
tokens := 'this is a selector' findTokens: Character space.
tokens allButFirst
    inject: tokens first
    into: [:selector :token | selector, token capitalized]

How about this?

| tokens |
tokens := 'this is a selector' findTokens: Character space.
tokens allButFirst
    inject: tokens first
    into: [:selector :token | selector, token capitalized]
梦里梦着梦中梦 2024-10-25 15:50:56

我认为没有现有的方法可以做到这一点。

这是解决您的问题的一个实现:

input := 'hello how are you today'.
output := String streamContents: [ :stream |
    | capitalize |
    capitalize := false.
    input do: [ :char |
        char = Character space
            ifTrue: [ capitalize := true ]
            ifFalse: [
                stream nextPut: (capitalize
                    ifTrue: [ char asUppercase ]
                    ifFalse: [ char ]).
                capitalize := false ] ] ].

编辑:注意,与 Frank 的解决方案相比,这个解决方案更长,但它不会因空输入而中断,并且不会为每个创建一个新的字符串实例 步骤,因为它流过输入,这更有效(如果您有大字符串)。

I don't think there's an existing method doing this.

Here's an implementation that solves your problem:

input := 'hello how are you today'.
output := String streamContents: [ :stream |
    | capitalize |
    capitalize := false.
    input do: [ :char |
        char = Character space
            ifTrue: [ capitalize := true ]
            ifFalse: [
                stream nextPut: (capitalize
                    ifTrue: [ char asUppercase ]
                    ifFalse: [ char ]).
                capitalize := false ] ] ].

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).

留一抹残留的笑 2024-10-25 15:50:56

你没有说你正在使用哪个版本的 Pharo,但在稳定的 5.0 中,

'hello world this is a selector' asCamelCase asValidSelector

会产生

helloWorldThisIsASelector

To get what I'm using run:

curl get.pharo.org/50+vm | bash 

You don't say which version of Pharo you're using, but in the stable 5.0,

'hello world this is a selector' asCamelCase asValidSelector

yields

helloWorldThisIsASelector

To get what I'm using run:

curl get.pharo.org/50+vm | bash 
别想她 2024-10-25 15:50:56

我知道这很旧,但 Squeak 有一个有用的实现(String>>asCamelCase),它基本上是这样做的:

(String
    streamContents: [:stream | 'hello world' substrings
            do: [:sub | stream nextPutAll: sub capitalized]]) asLegalSelector

I know this is old but Squeak has a useful implementation (String>>asCamelCase) which basically does this:

(String
    streamContents: [:stream | 'hello world' substrings
            do: [:sub | stream nextPutAll: sub capitalized]]) asLegalSelector
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文