Solved

Simple String question

Posted on 2004-08-18
11
252 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
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 
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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

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…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled

733 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