4
\$\begingroup\$

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();
 }
}
Jamal
35.2k13 gold badges134 silver badges238 bronze badges
asked May 22, 2014 at 0:56
\$\endgroup\$

2 Answers 2

2
\$\begingroup\$
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 it static.

  • RepeatAString is a weir name for a method. I would call it Repeat or RepeatCharacters.

  • 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.

answered May 22, 2014 at 9:50
\$\endgroup\$
3
\$\begingroup\$

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)));
}
answered May 22, 2014 at 7:34
\$\endgroup\$
1
  • 1
    \$\begingroup\$ You mean, LINQ doesn't bring anything except for shortening the code significantly? \$\endgroup\$ Commented May 22, 2014 at 9:29

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.