【问题标题】:Split string into multiple Textboxes in Windows Phone在 Windows Phone 中将字符串拆分为多个文本框
【发布时间】:2015-03-27 19:30:10
【问题描述】:

由于 WPhone 中的 UIElements 有 2048x2048 pixel 限制,我正在尝试拆分一个太长而无法显示的字符串。

我已经尝试实现 ScrollableTextBlockdone here 但没有成功。所以我正在努力以另一种方式做到这一点。

我尝试在文本中使用通配符,特别是\r\n:当达到它时,使用Regex 完成的方法Splitter 返回剩余的子字符串:

private string Splitter(string str1)
    {
        MatchCollection matches = Regex.Matches(str1,"\r\n");
        int count = matches.Count / 2; //i want to split every text in half avoiding word truncating, 
        //so I'm getting the NewLine closest to the middle of the text
        int pos= matches[count].Index; //I get the index of the char in str1
        return str1.Substring(pos); 
    }

但是在到达matches[count] 时它给了我 ArgumentIsNullException。

我该如何解决这个问题?

【问题讨论】:

  • 你为什么使用正则表达式?为什么不只是str1.Split("\r\n")?不幸的是,您的问题描述不是很清楚。您应该提供可靠地重现问题的a good, minimal, complete code example,并且您应该提供完整异常信息(消息、堆栈跟踪以及对程序语句发生位置的清晰、明确的描述)。
  • 其实代码可以正常工作。使用输入字符串1 line\r\n1 line\r\n3 line\r\n4 line\r\n5 line\r\n6 line,结果为\n4 line\r\n5 line\r\n6 line。或许,您应该首先检查 str1 是否不为空或 null,然后是否找到匹配项。
  • @stribizhev 是的,抱歉@Peter Duniho 没有正确解释自己。无论如何,我最终使用了@Rufus L 解决方案,奇怪的是字符串文本上根本找不到通配符\r\n,这很奇怪。 Regex.Matches中搜索到的字符串有分词格式吗?
  • @RiccardoLomazzi:请检查我的回答。顺便说一句,您现在可以投票了,恭喜! :)

标签: c# regex string windows-phone-8 split


【解决方案1】:

我认为您可能只需要对您的论点和matches.Count 进行一些验证:

private static string Splitter(string str1)
{
    if (string.IsNullOrWhiteSpace(str1)) return str1;
    MatchCollection matches = Regex.Matches(str1, "\r\n");

    if (matches.Count == 0) return str1;

    int count = matches.Count / 2;
    int pos = matches[count].Index;
    return str1.Substring(pos);
}

【讨论】:

    【解决方案2】:

    Rufus 的代码存在一个潜在问题,即可能存在不同的换行符或序列。我见过带有\r\n\n 的文件。因此,我宁愿使用一个正则表达式来捕获最可能出现的事件 - [\r\n]+。我怀疑您的输入文件也只包含 \n 换行符。

    private static string Splitter(string str1)
    {
        if (string.IsNullOrWhiteSpace(str1)) return str1;
        var matches = Regex.Matches(str1, @"[\r\n]+");
    
        if (matches.Count == 0)
           return str1;
        else
        {
           var count = matches.Count / 2;
           var pos = matches[count].Index;
           return str1.Substring(pos);
        }
    }
    
    // Input: "111\n2222\n3333"
    // Output: "\n3333"
    

    【讨论】:

    • @RufusL:感谢您的告知,我决定编辑我的解释。
    【解决方案3】:

    抱歉回复晚了。我最终使用了.Split 方法,因为Regex 并不总是在我的字符串中找到\r\n,这很奇怪(字符串是从SQLite 数据库加载的,可能与UTF-8 编码有关?) 无论如何,新的Splitter 方法可能效率低下且原始,但有效:它将文本分成两个TextBlocks。反正我发了,也许有人也可以用。

    private void Splitter(string str)
        {
    
            StringBuilder build = new StringBuilder(); //it's more efficent for appending strings in a loop
            string [] words = str.Split(new string[] { @"\r\n" }, StringSplitOptions.None);
    
            int length=words.Length;
            int half = length / 2;
    
            //Appending first half
            for (int i = 0; i < half; i++)
            {
                build.Append(words[i]+"\r\n"); //reintroducing the \r\n because I need 
                                        //to put a System.Enviornment.NewLine in place
                                        //(and the Split method eliminates it)
            }
            txtTextBlock1.Text = build.ToString();
            build.Clear();
            //Appending second half
            for (int i = half; i < length; i++)
            {
                build.Append(words[i] + "\r\n");
            }
            txtTextBlock2.Text = build.ToString();
    
        }
    

    非常感谢大家的帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-04-28
      • 1970-01-01
      • 2013-05-28
      • 2016-02-21
      • 2011-06-14
      • 2017-11-21
      • 2012-10-25
      • 1970-01-01
      相关资源
      最近更新 更多