ASP.NET StreamWriter - x 逗号后换行
我有一个 JS 数组,它正在使用 StreamWriter 写入服务器上的文本文件。这是执行此操作的行:
sw.WriteLine(Request.Form["seatsArray"]);
此时正在写出一行,其中包含数组的全部内容。我希望每 5 个逗号后写入一个新行。示例数组:
BN,ST,A1,303,601,BN,ST,A2,303,621,BN,WC,A3,303,641,
应该输出:
BN,ST,A1,303,601,
BN,ST,A2,303,621,
BN,WC,A3,303,641,
我知道我可以使用字符串替换,但我只知道如何在每个逗号之后输出一个新行,而不是在指定数量的逗号之后输出。
我怎样才能让这件事发生?
谢谢!
I've got a JS array which is writing to a text file on the server using StreamWriter. This is the line that does it:
sw.WriteLine(Request.Form["seatsArray"]);
At the moment one line is being written out with the entire contents of the array on it. I want a new line to be written after every 5 commas. Example array:
BN,ST,A1,303,601,BN,ST,A2,303,621,BN,WC,A3,303,641,
Should output:
BN,ST,A1,303,601,
BN,ST,A2,303,621,
BN,WC,A3,303,641,
I know I could use a string replace but I only know how to make this output a new line after every comma, and not after a specified amount of commas.
How can I get this to happen?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好吧,这是我能想到的最简单答案:
它不是非常优雅,但我相信它会完成工作。
如果有必要,您可能希望随后完成当前行:
我确信有更聪明的方法......但这很简单。
一个问题:这些值总是三个字符长吗?因为如果是这样,你基本上只是每 20 个字符就将字符串分解一次......
Well, here's the simplest answer I can think of:
It's not terribly elegant, but it'll get the job done, I believe.
You may want this afterwards to finish off the current line, if necessary:
I'm sure there are cleverer ways... but this is simple.
One question: are the values always three characters long? Because if so, you're basically just breaking the string up every 20 characters...
比如:
我会尝试找到一个更优雅的解决方案。适当地加上一些功能风格。
更新:只是发现它实际上并不是您所需要的。这样你就得到了一个 3 行 5 列的 2D 数组。
然而这会给你 3 行。它们没有结尾“,”。你想要那个吗?您总是想打印出来吗?或者你想访问不同的行吗?:
或者通过这个相当大的代码。但我经常需要一个“Chunk”方法,以便它可以重用。我不知道是否有内置的“Chunk”方法 - 找不到它。
用法:
Something like:
I will try find a more elegant solution. Properly with some functional-style over it.
Update: Just find out it is not actually what you needs. With this you get a 2D array with 3 rows and 5 columns.
This however will give you 3 lines. They do not have a ending ','. Do you want that? Do you always want to print it out? Or do you want to have access to the different lines?:
Or by this rather large code. But I have often needed a "Chunk" method so it may be reusable. I do not know whether there is a build-in "Chunk" method - couldn't find it.
Usage: