关键代码:
#region文字换行/// <summary>/// 文字换行/// <para>eg:StringHelper.WrapText("YanZhiwei", 3);==>"Yan\r\nZhi\r\nwei"</para>/// </summary>/// <param name="data">需要换行的文字</param>/// <param name="maxWidth">多少长度换行</param>/// <returns>换行好的文字</returns>publicstaticstringWrapText(thisstringdata,intmaxWidth)
{int_stringCount = data.Length;if(maxWidth > 0 && _stringCount > maxWidth)
{
StringBuilder _builderString =newStringBuilder(data);int_breakCount = _builderString.Length / maxWidth;for(inti = 0; i < _breakCount; i++)
{int_insertPosition = i * maxWidth;if(_insertPosition != 0)
{int_offset = (i - 1) * 2;//(\r\n)_builderString.Insert(_insertPosition + _offset, Environment.NewLine);
}
}return_builderString.ToString();
}else{returndata;
}
}#endregion
测试:
[TestMethod()]publicvoidWrapTextTest()
{string_actual = StringHelper.WrapText("YanZhiwei", 3);
Assert.AreEqual<string>(@"Yan
Zhi
wei", _actual);
}
.csharpcode, .csharpcodePRe
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp{ background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }结果:
希望有所帮助!
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }