我想在gridview中显示产品的描述,但我想在一行上只显示15个字符,我想在15个字符后打破它,我写了countchar函数如下:
public int CountChars(string value)
{
bool lastWasSpace = false;
foreach (char c in value)
{
result++;
lastWasSpace = false;
}
return result;
}
并将函数称为:
string description="sdfsdfsd sdfsdf sdfsdf asdfsa dfsda safsaf sdfdf sdfs sdfsdf sdff sdf ";
CountChars(description);
我想查一下:
if(result>15)
{
after every 15 characters i want to break the line.
}
请告诉我怎么做。
答案 0 :(得分:0)
public string AddSpaces(string value) {
int result = 0;
string new_value = "";
foreach (char c in value)
{
result++;
new_value += c.ToString();
if ( result == 15 ) {
new_value += "<br />";
result = 0;
}
}
return new_value;
}
答案 1 :(得分:0)
if (value.Length > 15)
{
StringBuilder sb = new StringBuilder();
for (i = 0; i < value.Length; i+=15)
sb.Append(value, i, Math.Min(value.Length - i, 15)).Append("<br/>");
value = sb.ToString();
}
未经测试
答案 2 :(得分:0)
使用找到的方法here(拆分字符串)
string str = "111122223333444455";
int chunkSize = 4;
string tmp = String.Join("<br />", str.ToCharArray().Select((c, i) => new { Char = c, Index = i }).GroupBy(o => o.Index / 4).Select(g => new String(g.Select(o => o.Char).ToArray())).ToList());
Console.WriteLine(tmp);
它产生以下结果:
1111<br />2222<br />3333<br />4444<br />55