drduh44480
2018-09-04 22:13
浏览 403
已采纳

获取字符串数组中每个字符串的第一个字符

I am new to golang and I am trying to obtain the first character of a string in an array of strings. It seems like it would be easy, but I don't know how to approach it. Here is what I have done thus far:

package main

import (
    "fmt"
    "os"
    "strings"
)

func acronym(s string) (acr string) {

    // TODO: Your code here
    var arrayOfStrings []string
    arrayOfStrings = strings.Split(s, " ") //split string s into an array of strings based on space delimeter " "
    for _, str := range arrayOfStrings {
        fmt.Println(str)
    }
    return acr
}

func main() {
    s := "Pan Galactic Gargle Blaster"
    if len(os.Args) > 1 {
        s = strings.Join(os.Args, " ")
    }
    fmt.Println(acronym(s))
}

I want the resulting string to be PGGB I am a bit stuck as I have only looped through the array of strings, but i can't think of something like in java where one has the method/function charAt(). Thanks!

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

相关推荐 更多相似问题