Solved

Simple String question

Posted on 2004-08-18
11
246 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
 
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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This video discusses moving either the default database or any database to a new volume.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

706 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now