[英]Cut long words in c#
我必须使用“ xxx- xxx”在字符串中剪切任何比长度x长的字符串,以换行。
因此,例如:20个字符是可以的,但是当我的单词中有30个字符时,我必须将其切为18 +“-” + rest。
我写了这个方法,它以一个无限循环结束:
string temp = s;
string tempResult = "";
bool found = false;
do
{
found = false;
if (s.Length < lenght) return s;
else
{
//Examine every word to cut everything into words
string[] tempList = temp.Split(' ');
foreach (string temp2 in tempList)
{
//Check every word length now,
if (temp2.Length > lenght)
{
tempResult = tempResult + " " + temp2.Substring(0, lenght - 3) + "- " + temp2.Substring(lenght);
found = true;
}
else
tempResult = tempResult + " " + temp2;
}
if (found) temp = tempResult;
}
} while (found);
return tempResult;
如何为String
编写扩展方法(考虑单词边界)
var s = "abcd defghi abcd defghi".LimitTo(10);
public static string LimitTo(this string s, int maxLen)
{
string toEnd = "...";
if (s.Length > maxLen)
{
maxLen -= toEnd.Length;
while (!char.IsWhiteSpace(s[maxLen])) maxLen--;
s = s.Substring(0, maxLen) + toEnd;
}
return s;
}
但是,您要问的内容还不清楚 ,我想这可能是您想要的,并且更简单:
static void Main(string[] args)
{
string foo = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec blandit ligula dolor, tristique.";
Console.Write(Truncate(foo, 20));
Console.Read();
}
public static string Truncate(string text, int length)
{
int index = text.Length;
while (index > 0)
{
text = text.Insert(index, "- ");
index -= length;
}
return text;
}
这给出:
Lorem ipsum dol-或坐着,建议使用adipiscing elit-。 多尼斯克·多内克·布兰迪特·利古拉·多洛尔-
另外,由于不清楚您需要什么,因此产生不同的效果:
static void Main(string[] args)
{
string foo = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec blandit ligula dolor, tristique.";
Console.Write(Truncate(foo, 20));
Console.Read();
}
public static string Truncate(string text, int maxlength)
{
maxlength = maxlength - 2;//allow space for '- '
string truncated = string.Empty;
int lastSpace = 0;
if (text.Length > maxlength)
{
string temp = text.Substring(0, maxlength);
lastSpace = temp.LastIndexOf(" ");
truncated = temp.Substring(0, lastSpace);
}
else
{
return text;
}
return truncated.Trim().Insert(truncated.Length, "- ") + text.Substring(lastSpace);
}
给出:
Lorem ipsum主宰,奉献自若。 Donec blandit ligula dolor,三位一体。
尝试一些简单的方法:
string test = s; //Your string
int count = (int)Math.Floor((decimal) test.Length / 20);
for (int i = 0; i < count; i++)
{
test = test.Insert(((i + 1) * 20), "- ");
}
注意:这是一个基本示例,仅在字符串中每20个字符添加一个"- "
。
编辑-
如果您只想在前20个字符后拼接字符串:
s = s.Length > 20 ? s.Insert(18, "- ") : s;
通过使用扩展方法,您可以随时轻松地将任何字符串切成任意长度。
public static class MyExtensions
{
public static string CutStringAt(this string s, int length)
{
int len = s.Length;
if (len > length)
{
int pos = 0;
StringBuilder sb = new StringBuilder();
while (pos < len)
{
if ((len - pos) < length)
{
int left = len - pos;
sb.AppendLine(s.Substring(pos, left).Trim());
pos += left;
}
else
{
sb.AppendLine(s.Substring(pos, length).Trim());
pos += length;
}
}
s = sb.ToString();
}
return s;
}
}
使用此代码,您只需调用即可轻松剪切任何字符串
string aCutString = "This string is waaaaaay tooooo looong".CutStringAt(20);
我不太确定您的要求是什么。 我假设是这样的:
给定一个字符串,该字符串包含零个或多个用空格分隔的单词,请插入空格,以使字符串中没有单词长于指定字符数。
以下方法实现了该要求:
public string SplitLongWords(string text, int maxWordLength)
{
var result = new StringBuilder();
int currentWordLength = 0;
foreach (char c in text)
{
if (char.IsWhiteSpace(c))
{
currentWordLength = 0;
}
else if (currentWordLength == maxWordLength)
{
currentWordLength = 1;
result.Append(' '); // Or .Append('-') to separate long words with '-'
}
else
{
++currentWordLength;
}
result.Append(c);
}
return result.ToString().TrimEnd();
}
因此,鉴于此输入:
A AB ABC ABCD ABCDE ABCDEF ABCDEFG ABCDEFGH ABCDEFGHI ABCDEFGHJ
12345678901234567890
输出将是:
A AB ABC ABCD ABCD E ABCD EF ABCD EFG ABCD EFGH ABCD EFGHI ABCD EFGHJ
1234 5678 9012 3456 7890
尽管您的解决方案不是最佳解决方案,但是要解决此问题,您必须在do-while语句的开头添加tempResult =“”。 另外,请确保还将temp2.Substring(lenght)更改为temp2.Substring(lenght -3)并修剪最后的字符串,因为它的开头有空格:
string temp = s;
string tempResult = "";
bool found = false;
do
{
tempResult = "";
found = false;
if (s.Length < lenght) return s;
else
{
//Examine every word to cut everything into words
string[] tempList = temp.Split(' ');
foreach (string temp2 in tempList)
{
//Check every word length now,
if (temp2.Length > lenght)
{
tempResult = tempResult + " " + temp2.Substring(0, lenght - 3) + "- " + temp2.Substring(lenght -3);
found = true;
}
else
tempResult = tempResult + " " + temp2;
}
if (found) temp = tempResult;
}
} while (found);
return tempResult.TrimStart();
您可以简化解决方案并仅遍历长单词,而不是一遍又一遍地构建整个字符串:
protected string test() {
string s = "this is a test for realllllyyyyreallllyyyyloooooooongword";
string temp = s;
int lengthAllowed = 18;
string tempResult = "";
string temp3 = "";
if (s.Length < 18) return s;
else
{
//Untersuche jedes Wort, dazu schneide alles in Wörter
string[] tempList = temp.Split(' ');
foreach (string temp2 in tempList)
{
temp3 = temp2;
//Jetzt jedes Wort auf Länge prüfen,
while (temp3.Length > lengthAllowed)
{
tempResult = tempResult + temp3.Substring(0, lengthAllowed - 3) + "- ";
temp3 = temp3.Substring(lengthAllowed - 3);
}
tempResult = tempResult + temp3 + " ";
}
}
return tempResult.Substring(0,tempResult.Length-1);
}
这是基于以下假设:问题在于如果我们具有以下字符串:
this is a test for realllllyyyyreallllyyyyloooooooongword
结果是:
this is a test for realllllyyyyrea- llllyyyyloooooo- oongword
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.