Golang中如何获取字符串长度

Golang中如何获取字符串长度 你好,我想知道如何计算一个字符串的字节长度,以下是我的主代码:

package main

import "fmt"

func main() {

    const N = 10

    parole := make([]string, N)

    for i := 0; i < N; i++ {

        fmt.Scan(&parole[i])

    }

    fmt.Println(piuCorta(parole))
}

我需要编写 → func piuCorta(parole []string) int { ←,所以我写了这个函数:

func piuCorta(parole []string) int {

    count := 0

    for _, len := range parole {

        for _, letter := range len {

            if len < letter {

                parole = len

            }

        }

    }

return parole
}

但我知道这里面有一些错误……有人能提供一些建议吗?


更多关于Golang中如何获取字符串长度的实战教程也可以访问 https://www.itying.com/category-94-b0.html

7 回复

是什么类型的错误?

你实际上想做什么?

更多关于Golang中如何获取字符串长度的实战系列教程也可以访问 https://www.itying.com/category-94-b0.html


len([]rune(s)) 返回 Unicode 字符的数量,len(s) 返回字节的数量。

看起来,len(s)(其中s是一个string)返回的结果与len([]byte(s))相同,但len([]rune(s))返回的数值可能更小,但绝不会更大。

Go Playground - The Go Programming Language

Go Playground - The Go Programming Language

Ilenia_Salerno:

if len < letter {

问问你自己,lenletter 的类型是什么,以及你期望上面这个操作的结果是什么。顺便提一句,将内置函数的名称用作变量名是一种不好的做法。

if len < letter {

嗨,说实话,我不明白 piuCorta 在做什么

使用

for _, len := range parole {
    for _, letter := range len {
        if len < letter {
            ...
        }
    }
}

len 是一个字符串,而 letter 是一个 rune。你究竟想做什么?

package main

import "fmt"

func main() {

    const N = 10

    parole := make([]string, N)

    for i := 0; i < N; i++ {

        fmt.Scan(&parole[i])

    }

    fmt.Println(piuCorta(parole))
}

func piuCorta(parole []string) int {

    count := 0

    for _, len := range parole {

        for _, letter := range len {

            if len < letter {

                parole = len

                fmt.Printf("%#v has a lenght of %v bytes", count, len([]byte(s)))

            }

        }

    }

return parole
}

我按照你的指示操作了,但不知道为什么它报错。

在Go语言中获取字符串长度需要使用len()函数,它返回字符串的字节数。对于你的piuCorta函数,需要找到字符串切片中最短字符串的长度。

以下是正确的实现:

func piuCorta(parole []string) int {
    if len(parole) == 0 {
        return 0
    }
    
    // 初始化最短长度为第一个字符串的长度
    minLen := len(parole[0])
    
    // 遍历所有字符串,找到最短的长度
    for _, parola := range parole {
        currentLen := len(parola)
        if currentLen < minLen {
            minLen = currentLen
        }
    }
    
    return minLen
}

如果你需要计算字符串的字符数(而不是字节数),比如处理包含多字节字符(如中文)的字符串,应该使用utf8.RuneCountInString()

import "unicode/utf8"

func piuCortaCaratteri(parole []string) int {
    if len(parole) == 0 {
        return 0
    }
    
    minLen := utf8.RuneCountInString(parole[0])
    
    for _, parola := range parole {
        currentLen := utf8.RuneCountInString(parola)
        if currentLen < minLen {
            minLen = currentLen
        }
    }
    
    return minLen
}

示例使用:

package main

import (
    "fmt"
    "unicode/utf8"
)

func main() {
    parole := []string{"ciao", "hello", "你好", "a", "programmazione"}
    
    // 字节长度
    fmt.Println("最短字符串的字节长度:", piuCorta(parole))
    
    // 字符长度
    fmt.Println("最短字符串的字符长度:", piuCortaCaratteri(parole))
}

func piuCorta(parole []string) int {
    if len(parole) == 0 {
        return 0
    }
    
    minLen := len(parole[0])
    
    for _, parola := range parole {
        currentLen := len(parola)
        if currentLen < minLen {
            minLen = currentLen
        }
    }
    
    return minLen
}

func piuCortaCaratteri(parole []string) int {
    if len(parole) == 0 {
        return 0
    }
    
    minLen := utf8.RuneCountInString(parole[0])
    
    for _, parola := range parole {
        currentLen := utf8.RuneCountInString(parola)
        if currentLen < minLen {
            minLen = currentLen
        }
    }
    
    return minLen
}

输出:

最短字符串的字节长度: 1
最短字符串的字符长度: 1

在你的原始代码中,主要问题是:

  1. 变量命名冲突(len是Go的内置函数名)
  2. 逻辑错误:不应该在循环中修改输入参数parole
  3. 返回类型错误:函数声明返回int但尝试返回parole[]string类型)
回到顶部