The repeat
method in Julia returns a new string whose value is the concatenation of this string repeated n
times, where n
is passed as an argument.
repeat(String::AbstractString, n::Integer)
This method takes two arguments.
sourcestr
: This is the string to be repeated.n
: This is the integer value denoting the number of times the string is repeated.This method returns a string as a return value.
The code below demonstrates how we can use the repeat
method:
# Repeating the string 3 times println(repeat("Educative_", 3)) # Repeating the string 4 times println(repeat("**_", 4)) # Repeating the string 2 times println(repeat("hello ", 2))
Line 2: We use the repeat
method with the Educative_
string and 3
as arguments. The repeat
method returns
Educative_Educative_Educative_
string as a result.
Line 5: We use the repeat
method with the **_
string and 4
as arguments. The repeat
method returns
**_**_**_**_
as a result.
Line 8: We use the repeat
method with the hello
string and 2
as arguments. The repeat
method returns
hello hello
as a result.
RELATED TAGS
CONTRIBUTOR
View all Courses