简体   繁体   English

Golang 可以像 Python 那样乘以字符串吗?

[英]Can Golang multiply strings like Python can?

Python can multiply strings like so: Python 可以像这样乘以字符串:

Python 3.4.3 (default, Mar 26 2015, 22:03:40)
[GCC 4.9.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> x = 'my new text is this long'
>>> y = '#' * len(x)
>>> y
'########################'
>>>

Can Golang do the equivalent somehow? Golang 能以某种方式做同样的事情吗?

It has a function instead of an operator, strings.Repeat .它有一个函数而不是一个操作符, strings.Repeat Here's a port of your Python example, which you can run here :这是您的 Python 示例的一个端口,您可以在此处运行:

package main

import (
    "fmt"
    "strings"
    "unicode/utf8"
)

func main() {
    x := "my new text is this long"
    y := strings.Repeat("#", utf8.RuneCountInString(x))
    fmt.Println(x)
    fmt.Println(y)
}

Note that I've used utf8.RuneCountInString(x) instead of len(x) ;请注意,我使用了utf8.RuneCountInString(x)而不是len(x) the former counts "runes" (Unicode code points), while the latter counts bytes.前者计算“符文”(Unicode 代码点),而后者计算字节。 In the case of "my new text is this long" , the difference doesn't matter since all the characters are only one byte, but it's good to get into the habit of specifying what you mean:"my new text is this long"的情况下,差异并不重要,因为所有字符都只有一个字节,但养成指定您的意思的习惯是很好的:

len("ā") //=> 2
utf8.RuneCountInString("ā") //=> 1

Since this was a Python comparison question, note that in Python, the one function len counts different things depending on what you call it on.由于这是一个 Python 比较问题,请注意在 Python 中,one 函数len计算不同的内容,具体取决于您调用它的内容。 In Python 2, it counted bytes on plain strings and runes on Unicode strings ( u'...' ):在 Python 2 中,它计算纯字符串上的字节数和 Unicode 字符串上的符文 ( u'...' ):

Python 2.7.18 (default, Aug 15 2020, 17:03:20)
>>> len('ā') #=> 2
>>> len(u'ā') #=> 1

Whereas in modern Python, plain strings are Unicode strings:而在现代 Python 中,纯字符串Unicode 字符串:

Python 3.9.6 (default, Jun 29 2021, 19:36:19) 
>>> len('ā') #=> 1

If you want to count bytes, you need to encode the string into abytearray first:如果要计算字节数,则需要先将字符串编码为字节bytearray

>>> len('ā'.encode('UTF-8')) #=> 2

So Python has multiple types of string and one function to get their lengths;所以Python有多种类型的字符串和一个函数来获取它们的长度; Go has only one kind of string, but you have to pick the length function that matches the semantics you want. Go 只有一种字符串,但你必须选择与你想要的语义匹配的长度函数。

Yes, it can, although not with an operator but with a function in the standard library.是的,它可以,虽然不是使用运算符,而是使用标准库中的函数。

It would be very easy with a simple loop, but the standard library provides you a highly optimized version of it: strings.Repeat() .使用简单的循环会很容易,但标准库为您提供了一个高度优化的版本: strings.Repeat()

Your example:你的例子:

x := "my new text is this long"
y := strings.Repeat("#", len(x))
fmt.Println(y)

Try it on the Go Playground .Go Playground上试一试。

Notes: len(x) is the "bytes" length (number of bytes) of the string (in UTF-8 encoding, this is how Go stores strings in memory).注意: len(x)是字符串的“字节”长度(字节数)(在 UTF-8 编码中,这是 Go 在内存中存储字符串的方式)。 If you want the number of characters (runes), use utf8.RuneCountInString() .如果您想要字符数(符文),请使用utf8.RuneCountInString()

Yup.是的。 The strings package has a Repeat function .字符串包具有Repeat功能

you can use the string package. it has a repeat function here您可以使用字符串 package。它在这里重复 function

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM