Golang 可以像 Python 那样乘以字符串吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33139020/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can Golang multiply strings like Python can?
提问by Duke Dougal
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 能以某种方式做同样的事情吗?
采纳答案by Mark Reed
It has a function instead of an operator, strings.Repeat
. Here's a port of your Python example, which you can run here:
它有一个函数而不是一个运算符,strings.Repeat
。这是您的 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)
; the former counts "runes" (Unicode code points), while the latter counts bytes. 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:
请注意,我使用了utf8.RuneCountInString(x)
而不是len(x)
; 前者计算“符文”(Unicode 代码点),而后者计算字节。在 的情况下"my new text is this long"
,差异无关紧要,因为所有字符都只有一个字节,但是养成指定您的意思的习惯是很好的:
len("ā") //=> 2
utf8.RuneCountInString("ā") //=> 1
(In Python 2, len
counts bytes on plain strings and runes on Unicode strings (u'...'
):
(在 Python 2 中,len
计算纯字符串上的字节数和 Unicode 字符串上的符文 ( u'...'
):
>>> len('ā') #=> 2
>>> len(u'ā') #=> 1
In Python 3, plain strings areUnicode strings and len
counts runes; if you want to count bytes, you have to encode the string into a bytearray
first, which you can do with a literal using the b'...'
quoting syntax.:
在 Python 3 中,纯字符串是Unicode 字符串,并len
计算符文;如果要计算字节数,则必须将字符串编码为bytearray
第一个,您可以使用b'...'
引用语法对文字进行编码。:
>>> len('ā') #=> 1
>>> len(b'ā') #=> 2
In Go, there's only one kind of string. So you don't have to convert, but you do have to pick the function that matches the semantics you want.)
在 Go 中,只有一种字符串。所以你不必转换,但你必须选择与你想要的语义匹配的函数。)
回答by icza
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). If you want the number of characters (runes), use utf8.RuneCountInString()
.
注意:len(x)
是字符串的“字节”长度(字节数)(在 UTF-8 编码中,这是 Go 在内存中存储字符串的方式)。如果您想要字符数(符文),请使用utf8.RuneCountInString()
.
回答by ShadowRanger
Yup. The strings package has a Repeat
function.
是的。strings 包有一个Repeat
函数。