How to make the first character in a string lowercase?

20,580 views
Skip to first unread message

Nikolai

unread,
Nov 24, 2012, 5:51:23 AM11/24/12
to golan...@googlegroups.com
Hi!

What is the easiest way to make a string "LikeThis" --> "likeThis"?

Nikolai

unread,
Nov 24, 2012, 6:09:26 AM11/24/12
to golan...@googlegroups.com
I came up with this thingie, but I don't really like it:

Dan Kortschak

unread,
Nov 24, 2012, 6:09:15 AM11/24/12
to Nikolai, golan...@googlegroups.com
If it's all ASCII, this works http://play.golang.org/p/4GaLA5vbqM but you might want to use unicode.ToLower in the loop

On 24/11/2012, at 9:21 PM, "Nikolai" <niko...@gmail.com> wrote:

Hi!

What is the easiest way to make a string "LikeThis" --> "likeThis"?

--
 
 

Dan Kortschak

unread,
Nov 24, 2012, 6:11:20 AM11/24/12
to Dan Kortschak, Nikolai, golan...@googlegroups.com
Sorry mis-read second string.
--
 
 

Dan Kortschak

unread,
Nov 24, 2012, 6:17:29 AM11/24/12
to Dan Kortschak, Nikolai, golan...@googlegroups.com
http://play.golang.org/p/tu9Cum4EmP

On 24/11/2012, at 9:41 PM, "Dan Kortschak" <dan.ko...@adelaide.edu.au> wrote:

> Sorry mis-read second string.

Nikolai

unread,
Nov 24, 2012, 6:20:45 AM11/24/12
to golan...@googlegroups.com, Dan Kortschak, Nikolai
This one looks good! Thanks!

DisposaBoy

unread,
Nov 24, 2012, 7:22:30 AM11/24/12
to golan...@googlegroups.com, Dan Kortschak, Nikolai
You may also consider getting the first rune and then concatting it with the rest of the string which may slightly more efficient



 

steve wang

unread,
Nov 24, 2012, 9:26:47 AM11/24/12
to golan...@googlegroups.com
import "unicode"

func UpcaseInitial(str string) string {
    for i, v := range str {
        return string(unicode.ToUpper(v)) + str[i+1:]
    }
    return ""

steve wang

unread,
Nov 24, 2012, 9:31:26 AM11/24/12
to golan...@googlegroups.com
import "unicode"
func LowerInitial(str string) string {
    for i, v := range str {
        return string(unicode.ToLower(v)) + str[i+1:]

steve wang

unread,
Nov 24, 2012, 9:51:47 AM11/24/12
to golan...@googlegroups.com
Sorry, this doesn't work for multi-character string.
DisposaBoy's does.

Dan Kortschak

unread,
Nov 24, 2012, 3:17:34 PM11/24/12
to DisposaBoy, golan...@googlegroups.com, Nikolai
Why is that?

ujjwalt

unread,
Nov 24, 2012, 3:51:25 PM11/24/12
to golan...@googlegroups.com, DisposaBoy, Nikolai
Because unicode points(runes) can consist of multiple bytes. steve's implementation exits on encountering the first rune without any consideration for its width while DisposaBoy's implementation takes that into account  I am not sure since am new to this language and not familiar with its intricacies but this my thought.

DisposaBoy

unread,
Nov 24, 2012, 4:08:11 PM11/24/12
to golan...@googlegroups.com, DisposaBoy, Nikolai
Was this meant to be a reply to me, or steve wang? If to me, I'm not sure what you're asking
 

ujjwalt

unread,
Nov 24, 2012, 4:11:24 PM11/24/12
to golan...@googlegroups.com, DisposaBoy, Nikolai
No it was aimed at kortschak

Dan Kortschak

unread,
Nov 24, 2012, 4:25:00 PM11/24/12
to DisposaBoy, golan...@googlegroups.com, DisposaBoy, Nikolai
Sorry, thought that was clear: Why is appending the remainder of the string to the modified first rune more efficient that converting to a []rune and then converting back the modified slice.

ujjwalt's explanation that, in your example, only the first rune needed to be decoded and the remainder can then be appended without decoding makes sense.
--
 
 

DisposaBoy

unread,
Nov 24, 2012, 4:42:09 PM11/24/12
to golan...@googlegroups.com, DisposaBoy, Nikolai


On Saturday, November 24, 2012 9:29:34 PM UTC, kortschak wrote:
Sorry, thought that was clear: Why is appending the remainder of the string to the modified first rune more efficient that converting to a []rune and then converting back the modified slice.

ujjwalt's explanation that, in your example, only the first rune needed to be decoded and the remainder can then be appended without decoding makes sense.


Without checking, that'd be my assumption as well


[ 9 play | done: 12.037936s ]
lF likeThis
lA likeThis
benchLfStrA  5000000       722 ns/op
benchLaStrA  2000000       906 ns/op
benchLfStrB  2000000       828 ns/op
benchLaStrB   500000      4812 ns/op

narena...@gmail.com

unread,
Jul 13, 2016, 1:50:37 PM7/13/16
to golang-nuts


On Saturday, November 24, 2012 at 4:21:23 PM UTC+5:30, Nikolai wrote:
Hi!

What is the easiest way to make a string "LikeThis" --> "likeThis"?

Here is my answer

    bytestr := []byte(str)
   
for index, character := range bytestr {
       
char := string(character)
       
if char == strings.ToUpper(char) {
           
// Is lowercase
            bytestr
[index] = []byte(strings.ToLower(char))[0]
       
} else {
            bytestr
[index] = []byte(strings.ToUpper(char))[0]
       
}
   
}

Small hack of converting byte to string. Applying method re-converting to byte array. But we know it consists on ly one element. So index 0 will return you the modified byte.

Hope it is useful. Ugly but does the job for me.

Tamás Gulácsi

unread,
Jul 14, 2016, 12:26:13 AM7/14/16
to golang-nuts
But this works only for ASCII input!

Viktor Kojouharov

unread,
Jul 14, 2016, 4:56:44 AM7/14/16
to golang-nuts

John Souvestre

unread,
Jul 14, 2016, 4:18:20 PM7/14/16
to golang-nuts

What if the first character is a combined code point?

 

John

    John Souvestre - New Orleans LA

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Viktor Kojouharov

unread,
Jul 15, 2016, 3:28:20 AM7/15/16
to golang-nuts
Did you test it with combined code points?

John Souvestre

unread,
Jul 15, 2016, 4:32:44 AM7/15/16
to golang-nuts

Yes, I initially started the string with “A\u0312a”.  The combining code point appears over the second “a” rather than the first.  Hence my question.

 

But after thinking about it and doing some additional testing, this appears to be a problem with my setup (Chrome on Windows 7).  For example, when I use “A\u0313a” instead, it works as expected.

 

I tried about a dozen different combining points, and I found the same problem for 2 others (\u0321 and \u0322), but all the rest are OK.

 

Very strange.  J

dhana...@gmail.com

unread,
Oct 29, 2018, 10:30:55 AM10/29/18
to golang-nuts

Nikolai

unread,
Oct 29, 2018, 1:11:28 PM10/29/18
to golang-nuts
dude, you should test your code before posting ;)

messju mohr

unread,
Oct 29, 2018, 2:51:36 PM10/29/18
to golang-nuts
I don't know if that is "the easiest", but I would do something like this:
https://play.golang.org/p/DOhP5kc8rO4

dhananjay Verma

unread,
Jan 7, 2019, 12:16:53 AM1/7/19
to messju mohr, golang-nuts
thank you

--
You received this message because you are subscribed to a topic in the Google Groups "golang-nuts" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/golang-nuts/WfpmVDQFecU/unsubscribe.
To unsubscribe from this group and all its topics, send an email to golang-nuts...@googlegroups.com.

peterGo

unread,
Jan 7, 2019, 1:22:44 PM1/7/19
to golang-nuts
Nikolai,

"What is the easiest way to make a string "LikeThis" --> "likeThis"?"

Te easiest is not always the best.

The code should be correct and reasonably efficient. For example,

func firstToLower(s string) string {
    if len(s) > 0 {
        r, size := utf8.DecodeRuneInString(s)
        if r != utf8.RuneError || size > 1 {
            lo := unicode.ToLower(r)
            if lo != r {
                s = string(lo) + s[size:]
            }
        }
    }
    return s
}


$ go test tolow_test.go -bench=.
BenchmarkToLow-4    30000000    41.3 ns/op     8 B/op    1 allocs/op
BenchmarkLow-4     200000000     6.96 ns/op    0 B/op    0 allocs/op
$



Peter

robert engels

unread,
Jan 7, 2019, 1:45:50 PM1/7/19
to peterGo, golang-nuts
Huh? Isn’t your code calling the exact same method ?

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.

jake...@gmail.com

unread,
Jan 7, 2019, 4:14:36 PM1/7/19
to golang-nuts
I believe he is demostrating an optimization for strings that are already in the correct form.

robert engels

unread,
Jan 7, 2019, 4:32:42 PM1/7/19
to jake...@gmail.com, golang-nuts
yes, my bad, too cursory of a read of the code

jyoti...@gmail.com

unread,
May 31, 2020, 5:57:07 PM5/31/20
to golang-nuts
I came up with this:

package main

import (
"fmt"
"strings"
)

func main() {
fmt.Println(MakeFirstLowerCase("LikeThis"))
}

func MakeFirstLowerCase(s string) string {
    
s=strings.ToLower(string(s[0]))+s[1:]
return s

peterGo

unread,
May 31, 2020, 10:32:56 PM5/31/20
to golang-nuts
Your code only works when the first character is ASCII. For example, it does not work for the Greek alphabet, it does not work for an empty string.


Peter

naren.y...@tradebyte.com

unread,
Jun 18, 2020, 12:38:53 PM6/18/20
to golang-nuts
I think all other solutions works fine, but String Builder struct exists for the same reason.

package main

import (
   
"fmt"
   
"strings"    

)


func
ToLowerCase(str string) string {

   
var b strings.Builder    

    b
.WriteString(strings.ToLower(string(str[0])))    
    b
.WriteString(str[1:])
       
   
return b.String()

}

func main
() {
   
var str string = "GoLang"
    fmt
.Println(ToLowerCase(str))
}



Playground here: https://play.golang.org/p/aAyBGnM5p2x

leo

unread,
Jun 18, 2020, 4:18:23 PM6/18/20
to naren.y...@tradebyte.com, golang-nuts


package main

import (
"fmt"
"unicode"

)

func main() {
fmt.Println(MakeFirstLowerCase("LikeThis"))

}

func MakeFirstLowerCase(s string) string {
    if len(s)==0 {
       return s
    }  
   
    r := []rune(s)    
    r[0] = unicode.ToLower(r[0])
    return string(r)  
}

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.

Prithu Adhikary

unread,
Nov 4, 2022, 7:57:36 PM11/4/22
to golang-nuts
May be this?

func main() {
    myString := "LikeThis"
    print(strings.ToLower(myString[:1]) + myString[1:])
}

peterGo

unread,
Nov 4, 2022, 8:18:55 PM11/4/22
to golang-nuts
Prithu Adhikary,

As I pointed out earlier, your code only works when the first character is ASCII. For example, it does not work for the Greek alphabet, it does not work for an empty string.

https://groups.google.com/g/golang-nuts/c/WfpmVDQFecU/m/CQiT04mRAQAJ

Peter
Reply all
Reply to author
Forward
0 new messages