How to print char n times, like in C#?

Hello!
In C# I print char n times.
Console.WriteLine("{0}{1}",new string(’*’,2),new string(’@’,3)); // result **@@@

How to do this in Golang?

You would typically use a loop, which is probably what new string('@', 3) does in C#.

You can easily define a func repeatedChar(c rune, n int) string to fulfill the same purpose.

Thanks for the idea. So I found:

import "strings"
str1 := "*“
str2:=”@"
fmt.Println(strings.Repeat(str1,2) + strings.Repeat(str2,3)) //result **@@@

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.