Jump to content
  • Advertisement
Sign in to follow this  
dxFoo

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

This topic is 4846 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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
}
}

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!