How to convert camelcase string to underscored string

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
12 messages Options
12
Ep4
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

How to convert camelcase string to underscored string

Ep4
Is it possible to take a CamelCased string, and insert underscores using Groovy? So from 'HelloGoodBye' you'd end up with "hello_good_bye"

Thanks

C.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to convert camelcase string to underscored string

Martin C. Martin-2
I don't know of anything in Groovy itself for this, but I'll bet Grails
has something like that.

Best,
Martin

Christian1 wrote:
> Is it possible to take a CamelCased string, and insert underscores using
> Groovy? So from 'HelloGoodBye' you'd end up with "hello_good_bye"
>
> Thanks
>
> C.

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to convert camelcase string to underscored string

Dierk König
In reply to this post by Ep4
'HelloGoodBye'.replaceAll(/\B[A-Z]/) { '_' + it }.toLowerCase()

would make a nice example for ReGina ;-)

keep groovin'
Dierk

Am 22.07.2009 um 14:17 schrieb Christian1:

>
> Is it possible to take a CamelCased string, and insert underscores  
> using
> Groovy? So from 'HelloGoodBye' you'd end up with "hello_good_bye"
>
> Thanks
>
> C.
> --
> View this message in context: http://www.nabble.com/How-to-convert-camelcase-string-to-underscored-string-tp24604998p24604998.html
> Sent from the groovy - user mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>    http://xircles.codehaus.org/manage_email
>
>


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to convert camelcase string to underscored string

paulk_asert
In reply to this post by Ep4

println 'HelloGoodBye'.replaceAll('[A-Z]') {'_' + it.toLowerCase()} - '_'

Christian1 wrote:
> Is it possible to take a CamelCased string, and insert underscores using
> Groovy? So from 'HelloGoodBye' you'd end up with "hello_good_bye"
>
> Thanks
>
> C.


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to convert camelcase string to underscored string

Andreas Jöcker
In reply to this post by Dierk König
mhm - i think one of the first things Java is also nice compared to
groovy ;-)

"HelloGoodBye".replaceAll("\\B([A-Z])", "_$1").toLowerCase();


Am 22.07.2009 14:42, Dierk König schrieb:

> 'HelloGoodBye'.replaceAll(/\B[A-Z]/) { '_' + it }.toLowerCase()
>
> would make a nice example for ReGina ;-)
>
> keep groovin'
> Dierk
>
> Am 22.07.2009 um 14:17 schrieb Christian1:
>
>  
>> Is it possible to take a CamelCased string, and insert underscores  
>> using
>> Groovy? So from 'HelloGoodBye' you'd end up with "hello_good_bye"
>>
>> Thanks
>>
>> C.
>> --
>> View this message in context: http://www.nabble.com/How-to-convert-camelcase-string-to-underscored-string-tp24604998p24604998.html
>> Sent from the groovy - user mailing list archive at Nabble.com.
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe from this list, please visit:
>>
>>    http://xircles.codehaus.org/manage_email
>>
>>
>>    
>
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>     http://xircles.codehaus.org/manage_email
>
>
>  


--

Andreas Jöcker
GiS - Gesellschaft für integrierte Systemplanung mbH
Junkersstr. 2
69469 Weinheim

E-Mail   [hidden email]
Telefon +49 6201 503-59
Fax     +49 6201 503-66

Gesellschaft für integrierte Systemplanung mbH
Geschäftsführer: Eckhard Haffmann, Alfred Gai, Bernd Heselmann
Sitz der Gesellschaft: Zeppelinstr. 11 - 91052 Erlangen
Amtsgericht Fürth/Bayern - Handelsregister-Nr. HRB 3435


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to convert camelcase string to underscored string

paulk_asert
In reply to this post by paulk_asert

Or without the closure:

'HelloGoodBye'.replaceAll(/(\B[A-Z])/,'_$1').toLowerCase()

Paul King wrote:

>
> println 'HelloGoodBye'.replaceAll('[A-Z]') {'_' + it.toLowerCase()} - '_'
>
> Christian1 wrote:
>> Is it possible to take a CamelCased string, and insert underscores using
>> Groovy? So from 'HelloGoodBye' you'd end up with "hello_good_bye"
>>
>> Thanks
>>
>> C.
>
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>    http://xircles.codehaus.org/manage_email
>
>
>
>


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to convert camelcase string to underscored string

paulk_asert

You guys are too fast. Both my suggestions pipped by
similar ones 2 mins earlier!

Paul. ;-)

Paul King wrote:

>
> Or without the closure:
>
> 'HelloGoodBye'.replaceAll(/(\B[A-Z])/,'_$1').toLowerCase()
>
> Paul King wrote:
>>
>> println 'HelloGoodBye'.replaceAll('[A-Z]') {'_' + it.toLowerCase()} - '_'
>>
>> Christian1 wrote:
>>> Is it possible to take a CamelCased string, and insert underscores using
>>> Groovy? So from 'HelloGoodBye' you'd end up with "hello_good_bye"
>>>
>>> Thanks
>>>
>>> C.
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe from this list, please visit:
>>
>>    http://xircles.codehaus.org/manage_email
>>
>>
>>
>>
>
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>    http://xircles.codehaus.org/manage_email
>
>
>
>


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to convert camelcase string to underscored string

paulk_asert
In reply to this post by paulk_asert

Or without regex's:

'HelloGoodBye'.collect{ it in 'A'..'Z' ? '_' + it.toLowerCase() : it }.join()-'_'

Paul King wrote:

>
> Or without the closure:
>
> 'HelloGoodBye'.replaceAll(/(\B[A-Z])/,'_$1').toLowerCase()
>
> Paul King wrote:
>>
>> println 'HelloGoodBye'.replaceAll('[A-Z]') {'_' + it.toLowerCase()} - '_'
>>
>> Christian1 wrote:
>>> Is it possible to take a CamelCased string, and insert underscores using
>>> Groovy? So from 'HelloGoodBye' you'd end up with "hello_good_bye"
>>>
>>> Thanks
>>>
>>> C.
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe from this list, please visit:
>>
>>    http://xircles.codehaus.org/manage_email
>>
>>
>>
>>
>
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>    http://xircles.codehaus.org/manage_email
>
>
>
>


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to convert camelcase string to underscored string

Robert Fischer
In reply to this post by Ep4
str.replaceAll(/([^\p{Lu}]\p{Lu})/, {
  (it.length() > 1 ? "_" : "") + it.toLowerCase()
})

Have I mentioned Perl was my first great love?

~~ Robert.

Christian1 wrote:
> Is it possible to take a CamelCased string, and insert underscores using
> Groovy? So from 'HelloGoodBye' you'd end up with "hello_good_bye"
>
> Thanks
>
> C.

--
~~ Robert Fischer, Smokejumper IT Consulting.
Enfranchised Mind Blog http://EnfranchisedMind.com/blog

Check out my book, "Grails Persistence with GORM and GSQL"!
http://www.smokejumperit.com/redirect.html

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to convert camelcase string to underscored string

Robert Fischer
In reply to this post by paulk_asert
You're assuming upper case characters are between A..Z.  That's adorable.

Wrong, but adorable.

~~ Robert.

Paul King wrote:

>
> Or without regex's:
>
> 'HelloGoodBye'.collect{ it in 'A'..'Z' ? '_' + it.toLowerCase() : it
> }.join()-'_'
>
> Paul King wrote:
>>
>> Or without the closure:
>>
>> 'HelloGoodBye'.replaceAll(/(\B[A-Z])/,'_$1').toLowerCase()
>>
>> Paul King wrote:
>>>
>>> println 'HelloGoodBye'.replaceAll('[A-Z]') {'_' + it.toLowerCase()} -
>>> '_'
>>>
>>> Christian1 wrote:
>>>> Is it possible to take a CamelCased string, and insert underscores
>>>> using
>>>> Groovy? So from 'HelloGoodBye' you'd end up with "hello_good_bye"
>>>>
>>>> Thanks
>>>>
>>>> C.
>>>
>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe from this list, please visit:
>>>
>>>    http://xircles.codehaus.org/manage_email
>>>
>>>
>>>
>>>
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe from this list, please visit:
>>
>>    http://xircles.codehaus.org/manage_email
>>
>>
>>
>>
>
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>    http://xircles.codehaus.org/manage_email
>
>
>

--
~~ Robert Fischer, Smokejumper IT Consulting.
Enfranchised Mind Blog http://EnfranchisedMind.com/blog

Check out my book, "Grails Persistence with GORM and GSQL"!
http://www.smokejumperit.com/redirect.html

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


12
Loading...