Solved

Simple String question

Posted on 2004-08-18
11
247 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

863 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

19 Experts available now in Live!

Get 1:1 Help Now