c# - Writing \t to a file from user input -
i have started learning c# , i'm experimenting file io. having problem writing tab (\t
) character file.
this code far:
static void main(string[] args) { string[] input = console.readline().split(' '); console.writeline(string.join("\n", input)); file.writealllines(@"c:\users\shashank\desktop\test.txt", input); console.readkey(); }
when run script , input text:
hello \twhat \tis \tyour name
the following gets written file:
hello \twhat \tis \tyour name
but, want file output like:
hello name
i have looked online cannot find solution gives me desired result. tried using streamwriter
no avail.
there method unescaping strings in .net, although perhaps not expect it:
console.writeline(regex.unescape(@"\thello\nworld"));
will result in (depending on tab indentation setting):
hello world
so, if want unescape input string , split individual strings (lines) output, can this:
string[] input = regex.unescape( console.readline() ).split(' '); console.writeline(string.join("\n", input)); file.writealllines(@"c:\users\shashank\desktop\test.txt", input); console.readkey();
or, first split , unescape (using linq):
string[] input = console.readline().split(' ').select(regex.unescape).toarray(); console.writeline(string.join("\n", input)); file.writealllines(@"c:\users\shashank\desktop\test.txt", input); console.readkey();
if intention treat sequences of spaces single delimiter, use:
input.split(" ".tochararray(), stringsplitoptions.removeemptyentries)
Comments
Post a Comment