Skip to content

Instantly share code, notes, and snippets.

@jackmott jackmott/wordwrap.cs
Last active Nov 11, 2018

Embed
What would you like to do?
word wrap for monogame
public static string Wrap(string s, int width, Func<string, int> widthMeasure)
{
StringBuilder builder = new StringBuilder(s.Length);
var text = s.AsSpan();
int start = 0;
int len = 0;
for (int i = 0; i < text.Length; i++) {
if (text[i] == ' ')
{
if (widthMeasure(text.Slice(start, i-start).ToString()) <= width)
{
len = i - start;
}
else
{
builder.AppendLine(text.Slice(start,len).ToString());
start = i;
len = 0;
}
}
}
builder.AppendLine(text.Slice(start).ToString());
return builder.ToString();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.