3

What would an implementation of 'MagicFunction' look like to make the following (nunit) test pass?

public MagicFunction_Should_Prepend_Given_String_To_Each_Line()
{
    var str = @"line1
line2
line3";

    var result = MagicFunction(str, "-- ");

    var expected = @"-- line1
-- line2
-- line3";

    Assert.AreEqual(expected, result);
}

8 Answers 8

10
string MagicFunction(string str, string prepend)
{
   str = str.Replace("\n", "\n" + prepend);
   str = prepend + str;
   return str;
}

EDIT:
As others have pointed out, the newline characters vary between environments. If you're only planning to use this function on files that were created in the same environment then System.Environment will work fine. However, if you create a file on a Linux box and then transfer it over to a Windows box you'll want to specify a different type of newline. Since Linux uses \n and Windows uses \r\n this piece of code will work for both Windows and Linux files. If you're throwing Macs into the mix (\r) you'll have to come up with something a little more involved.

4
  • What about the "\r" character ? Sep 23, 2009 at 15:54
  • 8
    Or even better, how about Environment.NewLine. I'm pretty sure that's what it's there for. :) Sep 23, 2009 at 15:58
  • @SpencerRuport replace should be Replace instead (frankly it is not possible to suggest edit for less than 6 characters)
    – wondra
    Jan 11, 2018 at 9:31
  • @wondra - Fixed! Thanks. Jan 17, 2018 at 0:39
3

Use .Select on a list of the lines.

private static string MagicFunction(string str, string prefix)
{
    string[] lines = str.Split(new[] { '\n' });
    return string.Join("\n", lines.Select(s => prefix + s).ToArray());
}
2
  • You probably need to factor in \r\n - var lines = str.Split(new char[] {'\r','\n'}, StringSplitOptions.RemoveEmptyEntries);
    – Gishu
    Sep 23, 2009 at 16:35
  • @Gishu: I tested the method both with \r\n and \n linebreaks and it works with both. It does not work with only \r. One upside with splitting on \n only is that the resulting string contains the same style linebreaks as the input (even if there is a mixture between \r\n and \n linebreaks in the string. Sep 23, 2009 at 16:50
1

How about:

string MagicFunction(string InputText) {
    public static Regex regex = new Regex(
          "(^|\\r\\n)",
        RegexOptions.IgnoreCase
        | RegexOptions.CultureInvariant
        | RegexOptions.IgnorePatternWhitespace
        | RegexOptions.Compiled
        );

    // This is the replacement string
    public static string regexReplace = 
          "$1-- ";

    // Replace the matched text in the InputText using the replacement pattern
    string result = regex.Replace(InputText,regexReplace);

    return result;
}
1
var result = "-- " + str.Replace(Environment.NewLine, Environment.NewLine + "-- ");

if you want it cope with either Windows (\r\n) NewLines or Unix ones (\n) then:

var result = "-- " + str.Replace("\n", "\n-- ");

No need to touch the \r as it is to be left where it was before. If however you want to cross between Unix and Windows then:

var result = "-- " + str.Replace("\r","").Replace("\n", Enviornment.NewLine + "-- ");

Will do it and return the result in the local OS's format

2
  • @Thomas: The Environment.NewLine will work with whatever is the OS's NewLine string. The only issue is if parsing windows files on unix or vice versa
    – JDunkerley
    Sep 23, 2009 at 16:00
  • Yes, that's the issue I'm talking about... It's very common to manipulate UNIX file on Windows or vice versa Sep 23, 2009 at 16:04
1

You could do it like that :

public string MagicFunction2(string str, string prefix)
{
    bool first = true;
    using(StringWriter writer = new StringWriter())
    using(StringReader reader = new StringReader(str))
    {
        string line;
        while((line = reader.ReadLine()) != null)
        {
            if (!first)
                writer.WriteLine();
            writer.Write(prefix + line);
            first = false;
        }
        return writer.ToString();
    }
}
2
  • 1
    I just tried with "\r\n" (Windows), "\n" (Linux) and "\r" (Mac), it works in all cases. However it doesn't preserve the original new-line markers, it always uses Environment.NewLine in the output string Sep 23, 2009 at 16:03
  • Hmm, slight bug. It always adds a newline to the end of the string even if the original string didn't have one. Sep 23, 2009 at 17:04
0

You could split the string by Environment.NewLine, and then add the prefix to each of those string, and then join them by Environment.NewLine.

string MagicFunction(string prefix, string orignalString)
{
    List<string> prefixed = new List<string>();
    foreach (string s in orignalString.Split(new[]{Environment.NewLine}, StringSplitOptions.None))
    {
        prefixed.Add(prefix + s);
    }

    return String.Join(Environment.NewLine, prefixed.ToArray());
}
0

How about this. It uses StringBuilder in case you are planning on prepending a lot of lines.

string MagicFunction(string input)
{
  StringBuilder sb = new StringBuilder();
  StringReader sr = new StringReader(input);
  string line = null;

  using(StringReader sr = new StringReader(input))
  {
    while((line = sr.ReadLine()) != null)
    {
      sb.Append(String.Concat("-- ", line, System.Environment.NewLine));
    }
  }
  return sb.ToString();
}
0

Thanks all for your answers. I implemented the MagicFunction as an extension method. It leverages Thomas Levesque's answer but is enhanced to handle all major environments AND assumes you want the output string to use the same newline terminator of the input string.

I favored Thomas Levesque's answer (over Spencer Ruport's, Fredrik Mork's, Lazarus, and JDunkerley) because it was the best performing. I'll post performance results on my blog and link here later for those interested.

(Obviously, the function name of 'MagicFunctionIO' should be changed. I went with 'PrependEachLineWith')

public static string MagicFunctionIO(this string self, string prefix)
{
  string terminator = self.GetLineTerminator();
  using (StringWriter writer = new StringWriter())
  {
    using (StringReader reader = new StringReader(self))
    {
      bool first = true;
      string line;
      while ((line = reader.ReadLine()) != null)
      {
        if (!first)
          writer.Write(terminator);
        writer.Write(prefix + line);
        first = false;
      }
      return writer.ToString();
    }
  }
}

public static string GetLineTerminator(this string self)
{
  if (self.Contains("\r\n")) // windows
    return "\r\n";
  else if (self.Contains("\n")) // unix
    return "\n";
  else if (self.Contains("\r")) // mac
    return "\r";
  else // default, unknown env or no line terminators
    return Environment.NewLine;
}

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.