Split string into multiple lines
I have a long string of comments that I'd 开发者_运维问答like to split into multiple lines.
It's currently displayed as <%= Html.Encode(item.important_notes) %>
I've played with using .Substring
to split it, but can't figure out how to prevent it from splitting in the middle of a word. Instead of characters 1-100 on line 1 and 101-200 on line 2, I'd like to do something like character 1 through the last space before character 100 on line one. That character through the last space before the next 100 characters on line 2, etc.
What is the best way to do this?
EDIT: using ASP.NET-MVC
I would use a combination of substring and lastindexof. You get the last index of combined with a number to get the first space after your breakpoint.
StringBuilder sb = new StringBuilder();
while (base.Length > 100)
{
if (!base.Contains(" ")) { break;
sb.Append(base.Substring(0, base.Substring(0, 100).LastIndexOf(" ")));
//code to trim down base
sb.Append(/*newline*/);
}
sb.Append(base);
Written adhoc, but you get the idea.
Without being able to speak specifically to your issue, I would suggest you look into regular expressions. Regular expressions are able to handle complex text patterns with ease. You may have a split function available to you that takes just such a regular expression and returns an array of Strings.
精彩评论