Need help with MArgins/Padding on c# 3 posts

OP Tue May 15, 2018 5:02 am
User avatar
Little
Inner Family Member
  • Gender: Female
  • Posts: 340
  • Joined: Mar, 2018
  • Advertisements: 6
  • Karma: 33
Hello,

I'm working on an interactive story on c# but I'm having trouble with the margins on both left and right. If someone can help me to set the margins so that the words don't split in the middle, I would be very happy. I need the words to start a new line if they don't fit. Thank you so much.

Image


I added a pic of the issue I'm having when I send it to console.
Tue May 15, 2018 6:30 am
User avatar
Mafia Don
Legendary Family Member
  • Gender: Male
  • Posts: 12814
  • Joined: Nov, 2007
  • Advertisements: 406
  • Karma: 204
Wrote up something quick based off of a word wrap algorithm, but added additional parameter / logic for margins. I did not test this code, but it should give you a good idea. Heavily commented for ease of reading. Let me know if you have any questions.

Code
using System;
using System.Collections.Generic;

/// <summary>
///     Writes the specified data, followed by the current line terminator, to the standard output stream, while wrapping lines that would otherwise break words.
/// </summary>
/// <param name="paragraph">The value to write.</param>
/// <param name="tabSize">The value that indicates the column width of tab characters.</param>
/// <param name="intMargin">The value that indicates the margin width.</param>
public static void WriteLineWordWrap(string paragraph, int tabSize = 8, int intMargin)
{
   strMargin = "";
   
   //build Margin
   for (int i = 0; i < intMargin; i++)
      strMargin = strMargin + " ";
   
   //Store words into buffer
    string[] lines = paragraph
        .Replace("\t", new String(' ', tabSize))
        .Split(new string[] { Environment.NewLine }, StringSplitOptions.None);

   //Process buffer into a list for efficient calculations
    for (int i = 0; i < lines.Length; i++) {
        string process = lines[i];
        List<String> wrapped = new List<string>();

      //Measure window and calculate each line
        while (process.Length > Console.WindowWidth) {
            int wrapAt = process.LastIndexOf(' ', Math.Min(Console.WindowWidth - 1 - intMargin - intMargin, process.Length));
            if (wrapAt <= 0) break;

            wrapped.Add(process.Substring(0, wrapAt));
            process = process.Remove(0, wrapAt + 1);
        }

      //Print lines + margin
        foreach (string wrap in wrapped) {
            Console.WriteLine(strMargin + wrap + strMargin);
        }

        Console.WriteLine(process);
    }
}
OP Tue May 15, 2018 1:33 pm
User avatar
Little
Inner Family Member
  • Gender: Female
  • Posts: 340
  • Joined: Mar, 2018
  • Advertisements: 6
  • Karma: 33
Wow :) thank you so much
@Don Von Alpha Dom
. I'll try this out now!I love all the comments.
Everything I've seen online is just confusing. Not much explanation. I cant just punch in code. I actually need to know what's happening lol

Create an account or sign in to comment

You need to be a member in order to leave a comment

Sign in

Already have an account? Sign in here

SIGN IN NOW

Create an account

Sign up for a new account in our community. It's easy!

REGISTER A NEW ACCOUNT