强制字符换行CSS版和C#版

本文介绍了一种使用C#实现的字符串强制换行的方法,该方法能够根据指定的字符宽度进行换行处理,同时避免在单词中间进行截断。

强制字符换行。
style="word-break:break-all;word-wrap:break-word";
C#版:

None.gif
None.gif        
//oldString:要换行的字符串,width:每行的字符宽度(字符个数),tolerance:最长的单词长度,以免单词被从中截断
None.gif        
//spanWidth,每一行的宽度
None.gif
        public static String GetMutilLineString(String oldString, Int32 width, Int32 tolerance, Int32 spanWidth)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif{
InBlock.gif            
if (oldString == null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return null;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
if (oldString.Trim() == String.Empty)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return String.Empty;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
if (width <= 1)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                width 
= 150;
ExpandedSubBlockEnd.gif            }

InBlock.gif            Int32 iIndex 
= 0;
InBlock.gif            Int32 iFindLen 
= 0;
InBlock.gif            String strUnit 
= String.Empty;
InBlock.gif            String strFind 
= String.Empty;
InBlock.gif            String strReturn 
= String.Empty;
InBlock.gif            String strTemp 
= String.Empty;
InBlock.gif            Int32 iTolerance 
= (tolerance > 0? tolerance : 8;
InBlock.gif            Int32 iTempIndex 
= -1;
InBlock.gif            
while (oldString.Length > width + iTolerance)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                iFindLen 
= 2 * iTolerance;
InBlock.gif                strFind 
= oldString.Substring(width - iTolerance, iFindLen);
InBlock.gif
InBlock.gif                iIndex 
= strFind.LastIndexOf(",", iFindLen - 1);
InBlock.gif                
if (iIndex == -1)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    iIndex 
= strFind.LastIndexOf("'", iFindLen - 1);
ExpandedSubBlockEnd.gif                }

InBlock.gif                
if (iIndex == -1)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    iIndex 
= strFind.LastIndexOf(";", iFindLen - 1);
ExpandedSubBlockEnd.gif                }

InBlock.gif                
if (iIndex == -1)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    iIndex 
= strFind.LastIndexOf(".", iFindLen - 1);
ExpandedSubBlockEnd.gif                }

InBlock.gif                
if (iIndex == -1)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    iIndex 
= strFind.LastIndexOf(" ", iFindLen - 1);
ExpandedSubBlockEnd.gif                }

InBlock.gif                
if (iIndex == -1)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    iIndex 
= strFind.LastIndexOf("_", iFindLen - 1);
ExpandedSubBlockEnd.gif                }

InBlock.gif                
if (iIndex == -1)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    iIndex 
= strFind.LastIndexOf("-", iFindLen - 1);
ExpandedSubBlockEnd.gif                }

InBlock.gif                
if (iIndex == -1)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    strUnit 
= oldString.Substring(0, width);
InBlock.gif                    oldString 
= oldString.Remove(0, width);
ExpandedSubBlockEnd.gif                }

InBlock.gif                
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    strUnit 
= oldString.Substring(0, width - iTolerance + iIndex + 1);
InBlock.gif                    oldString 
= oldString.Remove(0, width - iTolerance + iIndex + 1);
ExpandedSubBlockEnd.gif                }

InBlock.gif                
while (strUnit.IndexOf("\r\n"> -1)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    iTempIndex 
= strUnit.IndexOf("\r\n");
InBlock.gif                    strTemp 
= strUnit.Substring(0, iTempIndex);
InBlock.gif                    strReturn 
+= String.Format("<tr width={0} style='word-wrap:break-word'><td>{1}</td></tr>", spanWidth, strTemp);
InBlock.gif                    strUnit 
= strUnit.Remove(0, iTempIndex + 2);
ExpandedSubBlockEnd.gif                }

InBlock.gif
InBlock.gif                strReturn 
+= String.Format("<tr width={0} style='word-wrap:break-word'><td>{1}</td></tr>", spanWidth, strUnit);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
while (oldString.IndexOf("\r\n"> -1)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                iTempIndex 
= oldString.IndexOf("\r\n");
InBlock.gif                strTemp 
= oldString.Substring(0, iTempIndex);
InBlock.gif                strReturn 
+= String.Format("<tr width={0} style='word-wrap:break-word'><td>{1}</td></tr>", spanWidth, strTemp);
InBlock.gif                oldString 
= oldString.Remove(0, iTempIndex + 2);
ExpandedSubBlockEnd.gif            }

InBlock.gif            strReturn 
+= String.Format("<tr width={0} style='word-wrap:break-word'><td>{1}</td></tr>", spanWidth, oldString);
InBlock.gif
InBlock.gif            strReturn 
= String.Format("<table>{0}</table>",strReturn);
InBlock.gif
InBlock.gif            
return strReturn;
ExpandedBlockEnd.gif        }

转载于:https://www.cnblogs.com/laihua/archive/2006/08/11/474770.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值