c# - 在 Windows Phone 中将字符串拆分为多个文本框

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

由于 WPhone 中的 UIElements 有 2048x2048 像素 限制,我试图拆分太长而无法显示的字符串。

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

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

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。

我该如何解决?

最佳答案

我认为您可能只需要对您的论点和 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);
}

关于c# - 在 Windows Phone 中将字符串拆分为多个文本框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29308747/

相关文章:

asp.net - ASP.NET 中的正则表达式验证器 for\*,\|,\^,\~

regex - 检查字符串是否包含数字或者是数字 - Thymeleaf

python - 我想要一个反斜杠 - 而不是两个

string - 如何在有引号的字符串周围加上引号?

c# - 我应该在哪里插入自定义 DefaultContractResolver JSON.NET?

java - 如何匹配具有直接特殊字符的行中的字符串?

mysql - mysql中的正则表达式捕获组

c# - 如何使用 C# 从图像中裁剪十字矩形?

c# - subject.OnError 抛出

c# - 通过 ProxyServer 发送 WCF 请求