【发布时间】: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