Solved

Simple String question

Posted on 2004-08-18
11
253 Views
Last Modified: 2008-01-09
I have a case where a string is too long and it's too big for the screen so I want to divide it up into two strings so I can diaply the first part on the first line and then the second part on the line right after it. for example..

string movieName = "the movie to end all movies ever made";

What I would like to do is after 15-20 characters spilt the string. I can use substring from this part and it's not that hard.
Here is the problem. I only want to split the string and display full words. Using the above example I want to display it like this..

the movie to end all
movies ever made

Instead of..

the movie to end a
ll movies ever made

See what I mean? How do I know that I'm breaking on a space... I don't think it's that hard, But I just don't know how to do it.
Help please.
thanks
Ghost
0
Comment
Question by:copyPasteGhost
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
11 Comments
 
LVL 7

Expert Comment

by:NipNFriar_Tuck
ID: 11831329
A quick way to do this is to use a RegEx...

using System.Text.RegularExpressions;

string movieName = "the movie to end all movies ever made";
Regex regex = new Regex(
    @"\w+",
    RegexOptions.IgnoreCase
    | RegexOptions.Multiline
    | RegexOptions.IgnorePatternWhitespace
    | RegexOptions.Compiled
    );

MatchCollection matches = regex.Match( movieName );
string[] myStrings = new string[#];
int count = 0;
int limit = 20;
foreach ( Match match in matches ) {
     if (string[count].Length + match.Value.Length > limit) {
          count++;
     }
     string[count] += match.Value;
}

So what is happening is the regex is breaking you line into complete words and storing them into a MatchCollection.  From there loop through the MatchCollection and assemble the text as you want...
0
 
LVL 13

Author Comment

by:copyPasteGhost
ID: 11831355
yes ok... but then I'll be looping on words. what if one word has 3 letters and aother word has 10 letters? I don't want to treat them the same way...
or maybe I didn't understand...
can you explain it to me please?
Ghost
0
 
LVL 10

Expert Comment

by:eternal_21
ID: 11831458
How is the text being written to the screen?  Is it a fixed-width font such as Courier or is it in a Console Application window?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 7

Accepted Solution

by:
NipNFriar_Tuck earned 500 total points
ID: 11831462
Sure, that is what the if is doing in the loop... I am checking the length of the current string plus the word, if the length is longer than the limit I put the word on a new string.  Once the loop is done I will have an array of strings with each string less than 20 chars!

Also, I forgot to put spaces between the words so the loop should look like:

foreach ( Match match in matches ) {
     if (string[count].Length + match.Value.Length > limit) {
          count++;
     } else {
          string[count] += " ";
     }
     string[count] += match.Value;
}
0
 
LVL 13

Author Comment

by:copyPasteGhost
ID: 11831526
So like this...

string movieName = "the movie to end all movies ever made";
            Regex regex = new Regex(
               @"\w+",
               RegexOptions.IgnoreCase
               | RegexOptions.Multiline
               | RegexOptions.IgnorePatternWhitespace
               | RegexOptions.Compiled
               );

            string[] myStrings = new string[3];
            MatchCollection matches = regex.Match( movieName );
           
            int count = 0;
            int limit = 20;
            foreach ( Match match in matches ) {
               if (myStrings[count].Length + match.Value.Length > limit) {
                  count++;
               } else {
                  myStrings[count] += " ";
               }
               myStrings[count] += match.Value;
            }

??
0
 
LVL 13

Author Comment

by:copyPasteGhost
ID: 11831652
I'm getting this error...

Cannot implicitly convert type 'System.Text.RegularExpressions.Match' to 'System.Text.RegularExpressions.MatchCollection'

ideas,
Ghost
0
 
LVL 7

Expert Comment

by:NipNFriar_Tuck
ID: 11831686
Sorry, regex.Match( movieName ) should actually be regex.Matches( movieName )
0
 
LVL 7

Expert Comment

by:NipNFriar_Tuck
ID: 11831700
Other than telling you the wrong function to use on regex what you posted should work...
0
 
LVL 13

Author Comment

by:copyPasteGhost
ID: 11831848
I';m getting a null pointer exception.:S
ideas?
Ghost
0
 
LVL 13

Author Comment

by:copyPasteGhost
ID: 11832013
umm
ok I fixed the null pointer.. but it's out putting...

Sky Captain and theWorldofTomorrow

instead of breaking it up.... it should be

Sky Captain and the
World of Tomorrow

thanks for your help,
Ghost
0
 
LVL 13

Author Comment

by:copyPasteGhost
ID: 11832056
my bad...
Sorry.
it's perfect.
thanks so much
Ghost
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

696 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question