37

In unit test I would like to hard code a block of lines as a string.

In C# I would do

var sb = new StringBuilder();
sb.AppendLine("myline1");
sb.AppendLine("myline2");
sb.AppendLine("myline3");

Since I converted to F# I tried to minimize the usage of .Net method by using bprintf instead, but somehow there is no bprintfn support which seems strange to me.

It is tedious to add \r\n at the end of each line manually.

Or is there any better way than StringBuilder?

1
  • Nowadays in C# we would use @"" :)
    – aloisdg
    Oct 17, 2018 at 18:58

7 Answers 7

107

Little known feature: you can indeed indent string content - by ending each line with a backslash. Leading spaces on the following line are stripped:

let poem = "The lesser world was daubed\n\
            By a colorist of modest skill\n\
            A master limned you in the finest inks\n\
            And with a fresh-cut quill.\n"

You will still need to include \n or \n\r at line ends though (as done in the example above), if you want these embedded in your final string.

Edit to answer @MiloDCs question:

To use with sprintf:

let buildPoem character =
    sprintf "The lesser world was daubed\n\
             By a colorist of modest skill\n\
             A master limned %s in the finest inks\n\
             And with a fresh-cut quill.\n" character

buildPoem "you"            
buildPoem "her"
buildPoem "him"
5
  • Good tip @Kit--that's a new one to me too. Jan 31, 2013 at 18:50
  • 3
    I don't like this approach because I guess it wouldn't use the platform-agnostic Environment.NewLine
    – knocte
    Apr 17, 2014 at 13:17
  • How to do this with sprintf?
    – MiloDC
    Jul 16, 2020 at 6:26
  • @MiloDC Edited to answer your question. ;-)
    – Kit
    Jul 17, 2020 at 8:19
  • With sprintf, I get an error when using formatters. sprintf "foo %s bar,\n\ then on the other line: foobar %s\n." "a" "b" (with indentation) is getting me expecting a string -> a -> b but given a string -> string. EDIT : wait, it compiles and work fine! The error is only in Visual Studio editor! Looks like a VS bug.
    – Pac0
    Nov 5, 2020 at 9:51
56

If you are under F# 3.0, triple-quoted strings may be the answer:

let x = """
myline1
myline2
myline3"""   
1
  • caveat with that approach is that you must "clip" the string lines at the beginning of line, otherwise the indentation spaces will be taken into the string. It looks a bit ugly when you have indented code,
    – Pac0
    Nov 5, 2020 at 10:38
15

I'm surprised nobody has mentioned this:

[ "My first line"
  "second line"
  "another line" ]
|> String.concat "\n"
2
  • it is basically the same as @jellyfish 's method, but looks extremely useful under such formatting.
    – colinfang
    Jan 31, 2013 at 20:45
  • this is IMO how triple quotes should have worked. If you want to write pretty code with triple quotes as it is now it just screws up the indentation.
    – user483036
    Aug 5, 2016 at 16:39
10

You can create directly multi-line string literals in F#:

let multiLineStr = 
  "myline1
myline2
myline3"

and C#:

var multiLineStr =
  @"myline1
myline2
myline3";
2
  • 1
    One drawback of this is, that you cannot indent the string content which looks bad.
    – colinfang
    Jan 29, 2013 at 15:40
  • The @ prefix can also be used in F#.
    – Abel
    Jul 17, 2020 at 11:28
8

I think there is no problem with using StringBuilder in F# as you did.

There is a function fprintfn in Printf module, so you can use it with a StringWriter object:

let sw = new StringWriter()
fprintfn sw "myline1"
fprintfn sw "myline2"
fprintfn sw "myline3"
sw.ToString()

I like fprintf and fprintfn since they are flexible. You can write to console output by supplying stdout instead.

2

You could define your own bprintfn function:

let bprintfn bldr fmt =
  (bldr, fmt) ||> Printf.kbprintf bldr.AppendLine

Or, to define a multi-line literal you could use triple-quotes as bytebuster suggested or a "verbatim literal," which begins with @ (see Strings on MSDN).

1
  • I think this gives myline1myline2myline3 - without the end lines
    – MiMo
    Jan 29, 2013 at 15:38
0

I'm out of touch with F#, but you might be able to do adapt my normal approach:

['line1', 'line2', 'line3'].join('\n'); //javascript

StringUtils.join(Arrays.asList("line1", "line2", "line3"), "\n")); // java, using Apache Commons Lang

Your Answer

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

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