For example, given the string "xyz", return the string "xxyyzz".
I was given this as a part of a test. I would really appreciate if you can help me to find a better way of doing it.
I came up with two methods to do the same thing but one was an extension method.
I have a couple more questions which I will post in separate posts.
public class RepeatCharactersInString
{
public string RepeatAString(string sInputString, int repeatCount)
{
StringBuilder sOutputString = new StringBuilder();
if (string.IsNullOrEmpty(sInputString))
{
Console.WriteLine("Empty string !");
}
else
{
foreach (char c in sInputString)
{
sOutputString.Append(new String(c, repeatCount));
}
}
return sOutputString.ToString();
}
}
// Method 2 Using extension methods
public static class RepeatCharactersInStingExtensions
{
public static string RepeatAllCharactersInThisString(this string sInputString, int repeatCount)
{
StringBuilder sOutputString = new StringBuilder();
if (string.IsNullOrEmpty(sInputString))
{
Console.WriteLine("Empty string !");
}
else
{
foreach (char c in sInputString)
{
sOutputString.Append(new String(c, repeatCount));
}
}
return sOutputString.ToString();
}
}
2 Answers 2
public class RepeatCharactersInString
public static class RepeatCharactersInStingExtensions
Those are weird class names. I don't think this method warrants its own class, so I would call the classes something like StringUtils
and StringExtensions
.
public string RepeatAString(string sInputString, int repeatCount)
This method should be
static
. If a method doesn't use any instance members and it doesn't have to be an instance method for another reason, it's usually best to make itstatic
.RepeatAString
is a weir name for a method. I would call itRepeat
orRepeatCharacters
.Don't use Hungarian notation for variables. You already specified the variable type once, there is no need to repeat it in its name. The right name for the first parameter would be
inputString
.The choice to have
repeatCount
is an interesting one. On one hand, the problem statement doesn't ask for it, so you're making your code more complicated unnecessarily (this is called You Aren't Gonna Need It or YAGNI). On the other hand, it's an extension that makes sense and doesn't require much code. In the end, I think it was a good choice.
if (string.IsNullOrEmpty(sInputString))
{
Console.WriteLine("Empty string !");
}
A library method like this one usually shouldn't write to the console. If you consider empty input invalid, throw an exception. If you consider it valid, just return an empty string.
sOutputString.Append(new String(c, repeatCount));
The premature optimizer in me says that you shouldn't allocate strings like this, it's inefficient. And since there is an overload of Append
that does exactly what you want without allocating a string, you should use that.
Also, string
is usually spelled in lowercase, just like you used elsewhere in the method.
No reason to write anything when string is empty.
public string RepeatAString(string sInputString, int repeatCount)
{
StringBuilder sOutputString = new StringBuilder();
foreach (char c in sInputString)
sOutputString.Append(new String(c, repeatCount));
return sOutputString.ToString();
}
I think your solution is the best. Obviously you can use LINQ if you want but IMO it doesn't bring anything to table.
public string RepeatAString(string input, int count)
{
return String.Concat(input.Select(c => new String(c, count)));
}
-
1\$\begingroup\$ You mean, LINQ doesn't bring anything except for shortening the code significantly? \$\endgroup\$svick– svick2014年05月22日 09:29:51 +00:00Commented May 22, 2014 at 9:29