FAQ
Hi!

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

--

Search Discussions

  • Nikolai at Nov 24, 2012 at 11:09 am
    I came up with this thingie, but I don't really like it:

    http://play.golang.org/p/ic5dxOKdcU
    On Saturday, November 24, 2012 12:51:23 PM UTC+2, Nikolai wrote:

    Hi!

    What is the easiest way to make a string "LikeThis" --> "likeThis"?
    --
  • Dan Kortschak at Nov 24, 2012 at 11:17 am
    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" wrote:

    Hi!

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

    --


    --
  • Dan Kortschak at Nov 24, 2012 at 11:11 am
    Sorry mis-read second string.

    On 24/11/2012, at 9:40 PM, "Dan Kortschak" wrote:

    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" wrote:

    Hi!

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

    --



    --


    --
  • Dan Kortschak at Nov 24, 2012 at 11:18 am
    http://play.golang.org/p/tu9Cum4EmP
    On 24/11/2012, at 9:41 PM, "Dan Kortschak" wrote:

    Sorry mis-read second string.
    --
  • Nikolai at Nov 24, 2012 at 11:20 am
    This one looks good! Thanks!
    On Saturday, November 24, 2012 1:18:41 PM UTC+2, kortschak wrote:

    http://play.golang.org/p/tu9Cum4EmP
    On 24/11/2012, at 9:41 PM, "Dan Kortschak" wrote:

    Sorry mis-read second string.
    --
  • DisposaBoy at Nov 24, 2012 at 12:22 pm

    On Saturday, November 24, 2012 11:20:45 AM UTC, Nikolai wrote:
    This one looks good! Thanks!
    On Saturday, November 24, 2012 1:18:41 PM UTC+2, kortschak wrote:

    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.
    You may also consider getting the first rune and then concatting it with
    the rest of the string which may slightly more efficient

    http://play.golang.org/p/D8cYDgfZr8




    --
  • Dan Kortschak at Nov 24, 2012 at 8:22 pm
    Why is that?
    On 24/11/2012, at 10:52 PM, "DisposaBoy" wrote:

    You may also consider getting the first rune and then concatting it with the rest of the string which may slightly more efficient

    http://play.golang.org/p/D8cYDgfZr8
    --
  • Ujjwalt at Nov 24, 2012 at 8:51 pm
    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.
    On Sunday, November 25, 2012 1:52:46 AM UTC+5:30, kortschak wrote:

    Why is that?

    On 24/11/2012, at 10:52 PM, "DisposaBoy" <dispo...@dby.me <javascript:>>
    wrote:
    You may also consider getting the first rune and then concatting it with
    the rest of the string which may slightly more efficient
    http://play.golang.org/p/D8cYDgfZr8
    --
  • DisposaBoy at Nov 24, 2012 at 9:08 pm

    On Saturday, November 24, 2012 8:22:46 PM UTC, kortschak wrote:
    Why is that?

    On 24/11/2012, at 10:52 PM, "DisposaBoy" <dispo...@dby.me <javascript:>>
    wrote:
    You may also consider getting the first rune and then concatting it with
    the rest of the string which may slightly more efficient
    http://play.golang.org/p/D8cYDgfZr8
    Was this meant to be a reply to me, or steve wang? If to me, I'm not sure
    what you're asking


    --
  • Ujjwalt at Nov 24, 2012 at 9:11 pm
    No it was aimed at kortschak
    On Sunday, November 25, 2012 2:38:11 AM UTC+5:30, DisposaBoy wrote:


    On Saturday, November 24, 2012 8:22:46 PM UTC, kortschak wrote:

    Why is that?
    On 24/11/2012, at 10:52 PM, "DisposaBoy" wrote:

    You may also consider getting the first rune and then concatting it
    with the rest of the string which may slightly more efficient
    http://play.golang.org/p/D8cYDgfZr8
    Was this meant to be a reply to me, or steve wang? If to me, I'm not sure
    what you're asking
    --
  • Dan Kortschak at Nov 24, 2012 at 9:29 pm
    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.

    On 25/11/2012, at 7:38 AM, "DisposaBoy" wrote:



    On Saturday, November 24, 2012 8:22:46 PM UTC, kortschak wrote:
    Why is that?
    On 24/11/2012, at 10:52 PM, "DisposaBoy" wrote:

    You may also consider getting the first rune and then concatting it with the rest of the string which may slightly more efficient

    http://play.golang.org/p/D8cYDgfZr8
    Was this meant to be a reply to me, or steve wang? If to me, I'm not sure what you're asking


    --


    --
  • DisposaBoy at Nov 24, 2012 at 9:42 pm

    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

    I did some benchmarks http://play.golang.org/p/xrF3JhxnOa

    [ 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


    On 25/11/2012, at 7:38 AM, "DisposaBoy" <dispo...@dby.me <javascript:>>
    wrote:


    On Saturday, November 24, 2012 8:22:46 PM UTC, kortschak wrote:

    Why is that?
    On 24/11/2012, at 10:52 PM, "DisposaBoy" wrote:

    You may also consider getting the first rune and then concatting it
    with the rest of the string which may slightly more efficient
    http://play.golang.org/p/D8cYDgfZr8
    Was this meant to be a reply to me, or steve wang? If to me, I'm not
    sure what you're asking


    --


    --
  • Steve wang at Nov 24, 2012 at 2:26 pm
    import "unicode"

    func UpcaseInitial(str string) string {

    for i, v := range str {

    return string(unicode.ToUpper(v)) + str[i+1:]

    }

    return ""

    }

    On Saturday, November 24, 2012 6:51:23 PM UTC+8, Nikolai wrote:

    Hi!

    What is the easiest way to make a string "LikeThis" --> "likeThis"?
    --
  • Steve wang at Nov 24, 2012 at 2:31 pm
    import "unicode"

    func LowerInitial(str string) string {

    for i, v := range str {

    return string(unicode.ToLower(v)) + str[i+1:]

    }

    return ""

    }

    On Saturday, November 24, 2012 10:26:47 PM UTC+8, steve wang wrote:

    import "unicode"

    func UpcaseInitial(str string) string {

    for i, v := range str {

    return string(unicode.ToUpper(v)) + str[i+1:]

    }

    return ""

    }

    On Saturday, November 24, 2012 6:51:23 PM UTC+8, Nikolai wrote:

    Hi!

    What is the easiest way to make a string "LikeThis" --> "likeThis"?
    --
  • Steve wang at Nov 24, 2012 at 2:51 pm
    Sorry, this doesn't work for multi-character string.
    DisposaBoy's does.
    On Saturday, November 24, 2012 10:31:26 PM UTC+8, steve wang wrote:

    import "unicode"

    func LowerInitial(str string) string {

    for i, v := range str {

    return string(unicode.ToLower(v)) + str[i+1:]

    }

    return ""

    }

    On Saturday, November 24, 2012 10:26:47 PM UTC+8, steve wang wrote:

    import "unicode"

    func UpcaseInitial(str string) string {

    for i, v := range str {

    return string(unicode.ToUpper(v)) + str[i+1:]

    }

    return ""

    }

    On Saturday, November 24, 2012 6:51:23 PM UTC+8, Nikolai wrote:

    Hi!

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

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedNov 24, '12 at 10:51a
activeNov 24, '12 at 9:42p
posts16
users5
websitegolang.org

People

Translate

site design / logo © 2021 Grokbase