将html字符串拆分为页面 [英] Split html string to page
本文介绍了将html字符串拆分为页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我的asp.net项目中,我需要向用户显示大文档.因为文件有很多文字,所以我需要使用分页.每页应包含约5000个符号.我想按逻辑标记(例如<br/>
nbsp
space
)分割页面.
At my asp.net project I need display big documents to user. Beacuse document has a lot of text, I need use paging. Every page should have about 5000 symbols. I want split pages by logical tokens such as <br/>
nbsp
space
.
最好的方法是什么?
谢谢
推荐答案
最简单的方法是为String
public static IEnumerable<string> GetPages(this string text,
int charsPerPage, string breakChar)
{
int count = 0;
int start = 0;
while (count < text.Length)
{
count = Math.Min(text.Length, count + charsPerPage);
if (count == text.Length)
{
yield return text.Substring(start, count - start);
}
else
{
var nextBreak = text.IndexOf(breakChar, count);
if (nextBreak == -1)
{
yield return text.Substring(start, count - start);
start = count + breakChar.Length;
}
else
{
yield return text.Substring(start, nextBreak - start);
start = nextBreak + breakChar.Length;
}
}
}
}
这可能无法正常工作,因为我没有正确测试它-但您明白了
This may not work exactly as I haven't properly tested it - but you get the idea
您可以像这样使用它
var pages = text.GetPages(5000, "<br/>");
这篇关于将html字符串拆分为页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文