Need help with MArgins/Padding on c#

Soli
by Soli · 3 posts
5 years ago in .Net (C#, VB, etc)
Posted 5 years ago · Author
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.
Posted 5 years ago
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);
    }
}
Posted 5 years ago · Author
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
Select a forum Protection     Help & Support     Introductions     Mafia News     IMVU News General Discussion     IMVU Lounge        IMVU Series / Roleplaying        Social Games     Mafia Market     Mafia Tools        Premium IMVU Tools        Off Topic Tools     Off Topic     Contests Creator Corner     Graphics Design        Photoshop        GIMP     Basic Creator Help     Catalog And Product Showcase     3D Meshing        3Ds Max        Sketchup        Blender Gangsters with Connections     White Hat Activities        Google Hacking        Trackers Programming Corner     Coding        Python        .Net (C#, VB, etc)        Flash        JAVA        Autoit        Batch        HTML & CSS        Javascript        PHP        Other        IMVU Homepage Codes           General           About me Panel           Messages Panel           Special Someone Panel           Visitors Panel           New Products Panel           Rankings Panel           Wishlist Panel           My Badges Panel           Outfits Panel           Url Panel           Groups Panel           Slideshow Panel           My Room Panel           Sandbox panel           Layouts     Help & Requests Free Credits     Approved Methods     Submit Methods Free Money     Approved Methods     Submit Methods Adult Corner     Get Mafia AP Here     AP Lounge        AP Social Games        Casual Dating Tips     IMVU Slave Market & Escorts