I want to be able to see what characters a string is composed of, so how do I print a string containing '\t' and '\n' as "\\t" and "\\n" respectively? That is, I want to go in the opposite direction, converting control chars to escape sequences.
Asked
Active
Viewed 44 times
0
-
loop over all characters and replace the control characters with their escaped version: (F#: ``"\nfoobar \thello world".ToCharArray() |> Array.fold (fun (acc : System.Text.StringBuilder) c -> match c with | '\n' -> acc.Append("\\n") | '\t' -> acc.Append("\\t") | _ -> acc.Append(c.ToString())) (new System.Text.StringBuilder()) |> fun sb -> sb.ToString()`` – BitTickler Jun 08 '15 at 00:36
-
@BitTickler the question was about C#, so an entire solution in F# isn't all that relevant, especially in the comments section. – PC Luddite Jun 08 '15 at 08:53
-
@pcluddite I saw it was marked duplicate and did not pursue the issue, given that there are better answers in the referenced question. I intended it to be a source of ideas or a specification if you like. F# is quite a good specification language, imho. – BitTickler Jun 08 '15 at 10:32