Cut long words in c#
i have to cut any string that is longer then lenght x in string with "xxx-
xxx" to make a linebreak.
So as example: with 20 chars its okay, when 30 chars in my word i must cut
it to 18 + "- " + rest.
I write this method which ends up in an endless loop
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;
No comments:
Post a Comment