C#: Help on making a string wrapper in a console app?

Started by
6 comments, last by kSquared 18 years, 8 months ago
How can I go about doing this? Using Console.WriteLine() cuts off characters if it exceeds the width of the console application instead of wrapping the entire word to the next line. Is there a simple way in doing this? Current Example: (character wrapped) You are in in a build ing. Desired Output: (word wrapped) You are in a building.
Advertisement
The simplest way is to make a function with the same method signature as the Console.WriteLine() overload you intend to use (probably Console.WriteLine(string)). Have this function do the word-wrapping and printing logic for you; then call that function when you want to print something.

A more elegant but harder solution is to create a delegate that can act as a proxy for the Decorator design pattern. This would enable you to, for instance, wrap strings that were text but not to wrap long floating-point numbers -- you can differentiate between things you'd like to wrap and things you don't want to wrap, or wrap certain things in different ways.
- k2"Choose a job you love, and you'll never have to work a day in your life." — Confucius"Logic will get you from A to B. Imagination will get you everywhere." — Albert Einstein"Money is the most egalitarian force in society. It confers power on whoever holds it." — Roger Starr{General Programming Forum FAQ} | {Blog/Journal} | {[email=kkaitan at gmail dot com]e-mail me[/email]} | {excellent webhosting}
I'm trying the simplest way. I suppose I'm just having trouble with the logic.

Update: No luck :(

[Edited by - dxFoo on August 10, 2005 5:38:50 PM]
When you say "no luck" what do you mean? Do you have no idea where to start, or have you tried something that didn't work?
No matter what I try, the words are still getting "cut up" at the max width of the console window. I can't get last full word to show up on the next line. For example, this is what I'm trying to solve...

        public static void wrap(string text)        {            for (int i = 0; i < text.Length; i++)            {                Console.Write(text);                // Get last full word in line - stuck here                // newline                // repeat until finished            }        }
Quote:Original post by dxFoo
No matter what I try, the words are still getting "cut up" at the max width of the console window. I can't get last full word to show up on the next line. For example, this is what I'm trying to solve...

*** Source Snippet Removed ***

Try this article. It's a bit more powerful than you'll need, but you should be able to adapt the code easily.
- k2"Choose a job you love, and you'll never have to work a day in your life." — Confucius"Logic will get you from A to B. Imagination will get you everywhere." — Albert Einstein"Money is the most egalitarian force in society. It confers power on whoever holds it." — Roger Starr{General Programming Forum FAQ} | {Blog/Journal} | {[email=kkaitan at gmail dot com]e-mail me[/email]} | {excellent webhosting}
A simple way is in the scenario where the string is too long start a for loop at the max screen width and decrement back until you get a space character. At that point you cut the string and writeline it, then do the same with the remaining string (it'll be a recursive function) until you've written the whole text out.
Blake's 7 RPG - a new project
Quote:Original post by Strewth
A simple way is in the scenario where the string is too long start a for loop at the max screen width and decrement back until you get a space character. At that point you cut the string and writeline it, then do the same with the remaining string (it'll be a recursive function) until you've written the whole text out.

Don't forget the special case -- what if the text is a very long string with no breaks (like a floating-point value) that won't fit on a single line? Your algorithm would give an infinite loop, since it never reaches a space (it would keep printing the subset of the string that it hadn't spaced over to yet, which is the entire string).
- k2"Choose a job you love, and you'll never have to work a day in your life." — Confucius"Logic will get you from A to B. Imagination will get you everywhere." — Albert Einstein"Money is the most egalitarian force in society. It confers power on whoever holds it." — Roger Starr{General Programming Forum FAQ} | {Blog/Journal} | {[email=kkaitan at gmail dot com]e-mail me[/email]} | {excellent webhosting}

This topic is closed to new replies.

Advertisement