I think you're bullshiting. anyway, here is something i
played with for
the past hour or so... it is not perfect but it is a first
step. right
now if it break a word into two it will put a dash. you need
to come up
with some way to break the words properly.
public string WrapEmUp(string text, int maxLineLetterCount)
{
if (text.Length <= maxLineLetterCount)
{
return text;
}
int currentIndex = 0;
int letterCount = 0;
string output = "";
foreach (char letter in text)
{
currentIndex++;
letterCount++;
output += letter;
int firstOffset=0, secondOffset=0;
GetSeparatorIndices(text, currentIndex-1,
ref
firstOffset, ref secondOffset);
if (letterCount + firstOffset <=
maxLineLetterCount &&
letterCount + secondOffset <=
maxLineLetterCount)
{
//nothing happends
}
else if (letter + firstOffset <=
maxLineLetterCount &&
letterCount + secondOffset >=
maxLineLetterCount)
{
if (!char.IsWhiteSpace(letter)
&&
!char.IsPunctuation(letter))
{
output += "-";
output +=
System.Environment.NewLine;
letterCount = 0;
}
}
else
{
if (!char.IsWhiteSpace(letter)
&&
!char.IsPunctuation(letter))
{
output += "-";
output +=
System.Environment.NewLine;
letterCount = 0;
}
else
{
output +=
System.Environment.NewLine;
letterCount = 0;
}
}
}
return output;
}
public void GetSeparatorIndices(string text, int
currentIndex,
ref int firstOffset, ref int secondOffset )
{
firstOffset = int.MinValue ;
secondOffset = int.MaxValue ;
string separatorLetters =
"qwrtplkjhgfdsmnbvcxz";
//we check whether we are the end of the text
if (currentIndex >= text.Length)
{
firstOffset = currentIndex;
return;
}
for (int letterIndex = currentIndex; letterIndex
<
text.Length; letterIndex++)
{
char letter = text[letterIndex];
if (char.IsPunctuation(letter))
{
if (firstOffset == int.MinValue)
{
firstOffset = letterIndex -
currentIndex;
}
else
{
secondOffset = letterIndex -
currentIndex ;
break;
}
}
if
(separatorLetters.IndexOf(char.ToLower(letter)) >
-1)
{
if (firstOffset == int.MinValue)
{
firstOffset = letterIndex -
currentIndex ;
}
else
{
secondOffset = letterIndex -
currentIndex ;
break;
}
}
if (char.IsWhiteSpace(letter))
{
if (firstOffset == int.MinValue)
{
firstOffset = letterIndex -
currentIndex ;
}
else
{
secondOffset = letterIndex -
currentIndex ;
break;
}
}
}
}
|