Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8145905
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T13:48:48+00:00 2026-06-06T13:48:48+00:00

I’m making a game, and I read dialogue text from an XML file. I’m

  • 0

I’m making a game, and I read dialogue text from an XML file. I’m trying to make a routine to add in newlines automatically as needed, so that it fits in the text box. It just won’t work right, though. Here’s the code as it currently is:

SpriteFont dialogueFont = font31Adelon;
int lastSpace = 0;
string builder = "";
string newestLine = "";
float maxWidth = 921.6f;
float stringLength = 0;

for (int i = 0; i <= speech.Length - 1; i++) //each char in the string
{
    if (speech[i] == ' ') //record the index of the most recent space
    {
        lastSpace = i;
    }

    builder += speech[i]; 
    newestLine += speech[i]; 
    stringLength = dialogueFont.MeasureString(newestLine).X;

    if (stringLength > maxWidth) //longer than allowed
    {
        builder = builder.Remove(lastSpace); //cut off from last space 
        builder += Environment.NewLine;
        i = lastSpace; //start back from the cutoff
        newestLine = "";
    }
}
speech = builder;

My test string is “This is an example of a long speech that has to be broken up into multiple lines correctly. It is several lines long and doesn’t really say anything of importance because it’s just example text.”

This is how speech ends up looking:
http://www.iaza.com/work/120627C/iaza11394935036400.png

The first line works because it happens to be a space that brings it over the limit, I think.

i = 81 and lastSpace = 80 is where the second line ends. builder looks like this before the .Remove command:
“This is an example of a long speech that\r\nhas to be broken up into multiple lines c”

and after it is run it looks like this:
“This is an example of a long speech that\r\nhas to be broken up into multiple line”

The third line goes over the size limit at i = 123 and lastSpace = 120. It looks like this before the .Remove:
“This is an example of a long speech that\r\nhas to be broken up into multiple line\r\ncorrectly. It is several lines long and doe”

and after:
“This is an example of a long speech that\r\nhas to be broken up into multiple line\r\ncorrectly. It is several lines long an”

As you can see, it cuts off an extra character, even though character 80, that space, is where it’s supposed to start removing. From what I’ve read .Remove, when called with a single parameter, cuts out everything including and after the given index. It’s cutting out i = 79 too, though! It seems like it should be easy enough to add or subtract from lastSpace to make up for this, but I either get “index out of bounds” errors, or I cut off even more characters. I’ve tried doing .Remove(lastSpace, i-lastSpace), and that doesn’t work either. I’ve tried handling “ends with a space” cases differently than others, by adding or subtracting from lastSpace. I’ve tried breaking things up in different ways, and none of it has worked.

I’m so tired of looking at this, any help would be appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T13:48:49+00:00Added an answer on June 6, 2026 at 1:48 pm

    You add Environment.NewLine to your StringBuilder, you need to consider that when you specify the index where to start removing.

    Environment.NewLine‘s value is System dependent. It can be "\r\n", "\n" or "\r" (or, only one of the first two according to MSDN).
    In your case it’s "\r\n", that means for removing one space, you added two other characters.

    First, you need to declare a new variable:

    int additionalChars = 0;
    

    Then, when adding a line of text, you should change your code to something like this:

    builder = builder.Remove(lastSpace + additionalChars); //cut off from last space 
    builder += Environment.NewLine;
    additionalChars += Environment.NewLine.Length - 1; 
    

    The -1 is because you already removed a space (should make this code independent of the system’s definition of Environment.NewLine).

    UPDATE: You should also account for words that are longer than the line limit. You should break them anyway (couldn’t help but have a try):

    if (stringLength > maxWidth) //longer than allowed
    {
        // Cut off only if there was a space to cut off at
        if (lastSpace >= 0) { 
            builder = builder.Remove(lastSpace + additionalChars); //cut off from last space 
            i = lastSpace; //start back from the cutoff
        }
        builder += Environment.NewLine;
        // If there was no space that we cut off, there is also no need to subtract it here
        additionalChars += Environment.NewLine.Length - (lastSpace >= 0 ? 1 : 0);
        lastSpace = -1; // Means: No space found yet
        newestLine = "";
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
In my XML file chapters tag has more chapter tag.i need to display chapters
I am trying to render a haml file in a javascript response like so:
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I have a text area in my form which accepts all possible characters from
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have a reasonable size flat file database of text documents mostly saved in
I'm parsing an XML file, the creators of it stuck in a bunch social
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
I have a bunch of posts stored in text files formatted in yaml/textile (from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.