Reputation:
I'm tasked with creating a function that takes two parameters of a string and int that will return a string that repeats the string parameter the int's number of times. This is what I came up with but getting an error. BTW, this is on CodeCardio that I do at work so I haven't been able to test it out in Xcode (my work sucks and uses Windows)
func repeater(aString: String, withNumber: Int) -> String {
let repeatedString = String(count: withNumber, repeatedValue: aString)
return repeatedString
}
Upvotes: 5
Views: 6761
Reputation: 11
Swift 5:
extension String {
func repeater(by count: Int) -> String {
return String(repeating: self, count: count)
}
}
Calling: "testString".repeater(by: counter)
Upvotes: 1
Reputation: 1693
As of Swift 3, init(repeating:count:)
can take String arguments as well.
var repeatingString = String(repeating: "ab", count: 7)
print(repeatingString) // ababababababab
Try this:
func repeater(aString: String, withNumber: Int) -> String {
let repeatedString = String(repeating: aString, count: withNumber)
return repeatedString
}
Upvotes: 9
Reputation: 2600
You can just do this:
func repeater(aString string: String, withNumber number: Int) -> String {
return [String](count: number, repeatedValue: string).reduce("", combine: +)
}
Upvotes: 4
Reputation:
func repeatAString(anyString: String, numberOfTimes: Int) ->String
{
var returnString: String = String()
for _ in 1...numberOfTimes
{
returnString = returnString.stringByAppendingString(anyString)
}
return returnString
}
Hi I have used stringByappendingString to solve this one. I see you have plenty of answers but no-one has suggested that so thought Id put it out there. Cheers!
Upvotes: 1
Reputation: 285082
The type of argument repeatedValue
of String(count: repeatedValue: aCharacter)
is a single Character
rather than String
.
You could use Array(count: withNumber, repeatedValue: aString)
and join the items
func repeater(aString: String, withNumber: UInt) -> String {
let repeatedArray = Array(count: Int(withNumber), repeatedValue: aString)
return repeatedArray.joinWithSeparator("")
}
Upvotes: 4
Reputation: 17544
I slightly modify Tanguy's answer. Now, the function will not crash if the second parameter is less than 0, but returns an empty string
func repeate(string: String, times: Int) -> String {
var repeatedString = ""
var n = times
while n > 0 {
repeatedString += string
n -= 1
}
return repeatedString
}
the same issue has Vadian's answer, checking the parameter and returning early with "" is probably the best solution. I personally prefer Vadian's approach.
Upvotes: 2
Reputation: 11
func repeater(aString: String, withNumber: Int) -> String
{
var repeatedString=""
for var index = 0; index < withNumber; ++index
{
repeatedString=repeatedString+aString
}
return repeatedString
}
for Function Call i use: print(repeater("String",withNumber: 5))
Upvotes: 0
Reputation: 1915
String(count: withNumber, repeatedValue: aString)
Is used to instantiate an string with a repeated character: Does Swift init(count:, repeatedValue:) work?
Instead do
func repeater(string: String, withNumber number: Int) -> String {
var repeatedString = String()
for i in 0..<number {
repeatedString += string
}
return repeatedString
}
Upvotes: 13