我必须用“xxx-xxx”剪切字符串中长度超过 x 的任何字符串以进行换行。
举个例子:20个字符没问题,但是当我的话有30个字符时,我必须把它剪成18个+“-”+休息。
我写了这个方法,最终进入一个无限循环:
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;