将文本块拆分为新行的字符串 [英] Breaking apart a textblock into strings at a new line

查看:29
本文介绍了将文本块拆分为新行的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取文本框的内容,并将其拆分为一个数组,然后将每一行写入一个文件.它看起来像这样:

I'm trying to get the content of a textbox, and split it into an array, and then write each line into a file. It looks something like this:

string[] ban = Regex.Split(ImportText.Text, "\r\n");

        foreach (string card in ban)
        {
            if (card != "")
            {
                string banlist = isoStorage.getSettings("banList");
                banlist = "\r\n" + card + banlist;
                isoStorage.changeSettings("banList", banlist);
            }
            ImportText.Text = "";
            updateBanListView();
        }

问题是文本框不会以 \r\n 的形式返回新行.我试过\n、\r\n,甚至environment.newline.我在 WP7 上使用 C#.

The problem is that textbox doesn't return a new line as \r\n. I've tried \n, \r\n, and even environment.newline. I'm using C# on WP7.

关于如何将我的文本框拆分为一系列行的任何想法?

Any ideas on how to split my textbox into an array of lines?

推荐答案

出于某种原因,Windows Phone 仅对 TextBox 中的行拆分使用回车符 (\r).有两种方法可以解决.

For some reason, Windows Phone only uses a carraige return (\r) for a line split in TextBoxes. Two ways you could go about it are.

string[] ban = ImportText.Text.Split('\r');

但如果他们决定在未来实际使用 \n,这可能会中断.我见过另一个例子 在这里这样做:

But that may break if they decide to actually use \n in the future. I've seen another example here doing it this way:

List<string> ban = new List<string>();
string s;

while ((s = reader.ReadLine()) != null)
{
    ban.Add(s);
}

感觉有点安全

这篇关于将文本块拆分为新行的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆