Solved

Simple String question

Posted on 2004-08-18
11
249 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
  • 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

777 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