ASP.NET StreamWriter - x 逗号后换行

发布于 2024-09-12 17:06:12 字数 445 浏览 7 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

笑咖 2024-09-19 17:06:12

好吧,这是我能想到的最简单答案:

string[] bits = Request.Form["seatsArray"].Split(',');

for (int i = 0; i < bits.Length; i++)
{
    sw.Write(bits[i]);
    sw.Write(",");
    if (i % 5 == 4)
    {
        sw.WriteLine();
    }
}

它不是非常优雅,但我相信它会完成工作。

如果有必要,您可能希望随后完成当前行:

if (bits[i].Length % 5 != 0)
{
    sw.WriteLine();
}

我确信有更聪明的方法......但这很简单。

一个问题:这些值总是三个字符长吗?因为如果是这样,你基本上只是每 20 个字符就将字符串分解一次......

Well, here's the simplest answer I can think of:

string[] bits = Request.Form["seatsArray"].Split(',');

for (int i = 0; i < bits.Length; i++)
{
    sw.Write(bits[i]);
    sw.Write(",");
    if (i % 5 == 4)
    {
        sw.WriteLine();
    }
}

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:

if (bits[i].Length % 5 != 0)
{
    sw.WriteLine();
}

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...

平定天下 2024-09-19 17:06:12

比如:

var input = "BN,ST,A1,303,601,BN,ST,A2,303,621,BN,WC,A3,303,641,";
var splitted = input.Split(',');

var cols = 5;
var rows = splitted.Length / cols;

var arr = new string[rows, cols];

for (int row = 0; row < rows; row++)
    for (int col = 0; col < cols; col++)
        arr[row, col] = splitted[row * cols + col];

我会尝试找到一个更优雅的解决方案。适当地加上一些功能风格。

更新:只是发现它实际上并不是您所需要的。这样你就得到了一个 3 行 5 列的 2D 数组。

然而这会给你 3 行。它们没有结尾“,”。你想要那个吗?您总是想打印出来吗?或者你想访问不同的行吗?:

var splitted = input.Split(new [] { ','}, StringSplitOptions.RemoveEmptyEntries); 

var lines = from item in splitted.Select((part, i) => new { part, i })
            group item by item.i / 5 into g
            select string.Join(",", g.Select(a => a.part));

或者通过这个相当大的代码。但我经常需要一个“Chunk”方法,以便它可以重用。我不知道是否有内置的“Chunk”方法 - 找不到它。

public static class LinqExtensions
{
    public static IEnumerable<IList<T>> Chunks<T>(this IEnumerable<T> xs, int size)
    {
        int i = 0;

        var curr = new List<T>();

        foreach (var x in xs)
        {
            curr.Add(x);

            if (++i % size == 0)
            {
                yield return curr;
                curr = new List<T>();
            }
        }
    }
}

用法:

var lines = input.Split(',').Chunks(5).Select(list => string.Join(",", list));

Something like:

var input = "BN,ST,A1,303,601,BN,ST,A2,303,621,BN,WC,A3,303,641,";
var splitted = input.Split(',');

var cols = 5;
var rows = splitted.Length / cols;

var arr = new string[rows, cols];

for (int row = 0; row < rows; row++)
    for (int col = 0; col < cols; col++)
        arr[row, col] = splitted[row * cols + col];

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?:

var splitted = input.Split(new [] { ','}, StringSplitOptions.RemoveEmptyEntries); 

var lines = from item in splitted.Select((part, i) => new { part, i })
            group item by item.i / 5 into g
            select string.Join(",", g.Select(a => a.part));

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.

public static class LinqExtensions
{
    public static IEnumerable<IList<T>> Chunks<T>(this IEnumerable<T> xs, int size)
    {
        int i = 0;

        var curr = new List<T>();

        foreach (var x in xs)
        {
            curr.Add(x);

            if (++i % size == 0)
            {
                yield return curr;
                curr = new List<T>();
            }
        }
    }
}

Usage:

var lines = input.Split(',').Chunks(5).Select(list => string.Join(",", list));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文