将字符串转换为字符串切片类型

huangapple go评论83阅读模式
英文:

Convert string to slice of string type

问题

在下面的代码中,当我检查s[i]时,它给我返回的是二进制数而不是字符。代码仍然可以工作,但是我如何在仍然使用字符串类型s作为参数的情况下,让s[i]返回一个字符呢?

func main() {
    var ip string
    fmt.Println("Enter string:")
    fmt.Scanf("%s\n", &ip)
    ip = strings.ToLower(ip)
    fmt.Println(isP(ip))
}

//Function to test if the string entered is a Palindrome
func isP(s string) string {
    mid := len(s) / 2
    last := len(s) - 1
    for i := 0; i < mid; i++ {
        if s[i] != s[last-i] {
            return "NO. It's not a Palindrome."
        }
    }
    return "YES! You've entered a Palindrome"
}

在Go语言中,字符串是由字节组成的,因此s[i]返回的是字符串s中索引为i的字节。如果你想要返回字符而不是字节,你可以使用string(s[i])将字节转换为字符串。修改后的代码如下:

func main() {
    var ip string
    fmt.Println("Enter string:")
    fmt.Scanf("%s\n", &ip)
    ip = strings.ToLower(ip)
    fmt.Println(isP(ip))
}

//Function to test if the string entered is a Palindrome
func isP(s string) string {
    mid := len(s) / 2
    last := len(s) - 1
    for i := 0; i < mid; i++ {
        if string(s[i]) != string(s[last-i]) {
            return "NO. It's not a Palindrome."
        }
    }
    return "YES! You've entered a Palindrome"
}

这样修改后,s[i]将返回一个字符而不是字节。

英文:

In the code below when I inspect s[i] it gives me binary numbers instead of characters. Code still works but how can I get s[i] to return a character, while still using s type string as parameter?

func main() {
    
    	var ip string
    	fmt.Println(&quot;Enter string:&quot;)
    	fmt.Scanf(&quot;%s\n&quot;, &amp;ip)
    	ip = strings.ToLower(ip)
    	fmt.Println(isP(ip))
    }
    //Function to test if the string entered is a Palindrome
    
    func isP(s string) string {
    	mid := len(s) / 2
    	last := len(s) - 1
    	for i := 0; i &lt; mid; i++ {
    		if s[i] != s[last-i] {
    			return &quot;NO. It&#39;s not a Palimdrome.&quot;
    		}
    	}
    	return &quot;YES! You&#39;ve entered a Palindrome&quot;
    }

答案1

得分: 7

如果你想将 string 转换为 []string,你也可以直接使用 strings.Split(s, "")

英文:

If you want to convert string to a []string you can also just use strings.Split(s,&quot;&quot;)

答案2

得分: 2

例如,

package main

import (
    "fmt"
    "strings"
)

func isPalindrome(s string) bool {
    s = strings.ToLower(s)
    r := []rune(s)
    mid := len(r) / 2
    last := len(r) - 1
    for i := 0; i < mid; i++ {
        if r[i] != r[last-i] {
            return false
        }
    }
    return true
}

func main() {
    var s string
    fmt.Println("输入字符串:")
    fmt.Scanf("%s\n", &s)
    fmt.Println(s, isPalindrome(s))
}

输出:

输入字符串:
123
123 false
输入字符串:
121
121 true
输入字符串:
abba
abba true
输入字符串:
世界世
世界世 true

Go语言中的字符string是一个UTF-8编码的字符序列。UTF-8是一种可变长度的字符编码。s[i]是一个字节而不是一个字符。rune(码点)是一个字符。使用string(r)rune转换为可打印的string。例如,

package main

import (
    "fmt"
)

func main() {
    s := "Hello, 世界"
    for _, r := range s {
        fmt.Print(string(r))
    }
    fmt.Println()
}

输出:

Hello, 世界

参考资料:

Unicode - 维基百科

Go中的字符串、字节、rune和字符

英文:

For example,

package main

import (
	&quot;fmt&quot;
	&quot;strings&quot;
)

func isPalindrome(s string) bool {
	s = strings.ToLower(s)
	r := []rune(s)
	mid := len(r) / 2
	last := len(r) - 1
	for i := 0; i &lt; mid; i++ {
		if r[i] != r[last-i] {
			return false
		}
	}
	return true
}

func main() {
	var s string
	fmt.Println(&quot;Enter string:&quot;)
	fmt.Scanf(&quot;%s\n&quot;, &amp;s)
	fmt.Println(s, isPalindrome(s))
}

Output:

Enter string:
123
123 false
Enter string:
121
121 true
Enter string:
abba
abba true
Enter string:
世界世
世界世 true

A Go character string is a UTF-8 encoded sequence of characters. UTF-8 is variable length character encoding. s[i] is a byte not a character. A rune (code point) is a character. Use string(r) to convert a rune to a printable string. For example,

package main

import (
	&quot;fmt&quot;
)

func main() {
	s := &quot;Hello, 世界&quot;
	for _, r := range s {
		fmt.Print(string(r))
	}
	fmt.Println()
}

Output:

Hello, 世界

References:

Unicode - Wikipedia

Strings, bytes, runes and characters in Go

答案3

得分: 0

如上面的评论中提到的,你可以使用string(s[i])将其转换为字符,或者你可以使用fmt.Printf("%c", s[i])来打印由相应的 Unicode 代码点表示的字符。

英文:

As mentioned above this in the comments, you can use string(s[i]) to convert it to a character or you can use fmt.Printf(&quot;%c&quot;, s[i]) to print the character represented by the corresponding Unicode code point.

答案4

得分: 0

你可以使用string()函数来返回字符。

例如:fmt.Println(string(s[i]))

英文:

you can use string() to return the character.

example : fmt.Println(string(s[i]))

huangapple
  • 本文由 发表于 2017年3月6日 02:02:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/42611974.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定