强制字符换行。
style="word-break:break-all;word-wrap:break-word";
C#版:
![](/Images/OutliningIndicators/None.gif)
//oldString:要换行的字符串,width:每行的字符宽度(字符个数),tolerance:最长的单词长度,以免单词被从中截断
//spanWidth,每一行的宽度
public static String GetMutilLineString(String oldString, Int32 width, Int32 tolerance, Int32 spanWidth)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
if (oldString == null)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return null;
}
if (oldString.Trim() == String.Empty)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return String.Empty;
}
if (width <= 1)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
width = 150;
}
Int32 iIndex = 0;
Int32 iFindLen = 0;
String strUnit = String.Empty;
String strFind = String.Empty;
String strReturn = String.Empty;
String strTemp = String.Empty;
Int32 iTolerance = (tolerance > 0) ? tolerance : 8;
Int32 iTempIndex = -1;
while (oldString.Length > width + iTolerance)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
iFindLen = 2 * iTolerance;
strFind = oldString.Substring(width - iTolerance, iFindLen);
![](/Images/OutliningIndicators/InBlock.gif)
iIndex = strFind.LastIndexOf(",", iFindLen - 1);
if (iIndex == -1)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
iIndex = strFind.LastIndexOf("'", iFindLen - 1);
}
if (iIndex == -1)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
iIndex = strFind.LastIndexOf(";", iFindLen - 1);
}
if (iIndex == -1)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
iIndex = strFind.LastIndexOf(".", iFindLen - 1);
}
if (iIndex == -1)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
iIndex = strFind.LastIndexOf(" ", iFindLen - 1);
}
if (iIndex == -1)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
iIndex = strFind.LastIndexOf("_", iFindLen - 1);
}
if (iIndex == -1)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
iIndex = strFind.LastIndexOf("-", iFindLen - 1);
}
if (iIndex == -1)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
strUnit = oldString.Substring(0, width);
oldString = oldString.Remove(0, width);
}
else
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
strUnit = oldString.Substring(0, width - iTolerance + iIndex + 1);
oldString = oldString.Remove(0, width - iTolerance + iIndex + 1);
}
while (strUnit.IndexOf("\r\n") > -1)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
iTempIndex = strUnit.IndexOf("\r\n");
strTemp = strUnit.Substring(0, iTempIndex);
strReturn += String.Format("<tr width={0} style='word-wrap:break-word'><td>{1}</td></tr>", spanWidth, strTemp);
strUnit = strUnit.Remove(0, iTempIndex + 2);
}
![](/Images/OutliningIndicators/InBlock.gif)
strReturn += String.Format("<tr width={0} style='word-wrap:break-word'><td>{1}</td></tr>", spanWidth, strUnit);
}
while (oldString.IndexOf("\r\n") > -1)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
iTempIndex = oldString.IndexOf("\r\n");
strTemp = oldString.Substring(0, iTempIndex);
strReturn += String.Format("<tr width={0} style='word-wrap:break-word'><td>{1}</td></tr>", spanWidth, strTemp);
oldString = oldString.Remove(0, iTempIndex + 2);
}
strReturn += String.Format("<tr width={0} style='word-wrap:break-word'><td>{1}</td></tr>", spanWidth, oldString);
![](/Images/OutliningIndicators/InBlock.gif)
strReturn = String.Format("<table>{0}</table>",strReturn);
![](/Images/OutliningIndicators/InBlock.gif)
return strReturn;
}