string Haskell 字符串中的换行符?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5944055/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Newline in Haskell String?
提问by Ash
How can I create a newline inside a String? Is it possible without using IO ()
?
如何在字符串中创建换行符?不使用可以IO ()
吗?
formatRow :: Car -> String
formatRow (a, d:ds, c, x:xs) = a ++ " | " ++ x ++ concat xs ++ " | " ++ show c ++ " | " ++ d ++ concat ds ++ (show '\n')
回答by sepp2k
To create a string containing a newline, just write "\n"
.
要创建包含换行符的字符串,只需编写"\n"
.
If you run your program on Windows, it will automatically be convertedto "\r\n"
.
如果您在 Windows 上运行您的程序,它会自动转换为"\r\n"
.
Note that calling show
on it will escape the newline (or any other meta-characters), so don't do foo ++ (show "\n")
or foo ++ (show '\n')
- just use foo ++ "\n"
.
请注意,调用show
它会转义换行符(或任何其他元字符),所以不要这样做foo ++ (show "\n")
或foo ++ (show '\n')
- 只使用foo ++ "\n"
.
Also note that if you just evaluate a string expression in GHCi without using putStr
or putStrLn
, it will just call show
on it, so for example the string "foo\n"
will display as "foo\n"
in GHCi, but that does not change the fact that it's a string containing a newline and it will print that way, once you output it using putStr
.
另请注意,如果您只是在 GHCi 中计算字符串表达式而不使用putStr
or putStrLn
,它只会调用show
它,因此例如该字符串"foo\n"
将显示为"foo\n"
GHCi,但这不会改变它是一个包含换行符的字符串的事实,它一旦您使用putStr
.