473,383 Members | 1,748 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

String Formatting Problem - Need help in MS C#..

Say I have a string with a length of 30 chars, and I want to re-format that string into lines with each a maximum of 10 chars.
How do I format that text without changing the sentences involved in that string?
I mean, I want the text to lined up as I mentioned earlier - but I dont want the text to be messed up when the formatting is done....

Is there a way todo this?

/Regards, Geir G.
Dec 22 '07 #1
12 2078
CyberSoftHari
487 Expert 256MB
Doing homework here is prohibited and you have to try your self. I can just point you.

Expand|Select|Wrap|Line Numbers
  1. String strVariable;
  2. String[] strArray;
  3. strArray = strVariable.Split(char[] seperator, int Count);
Dec 22 '07 #2
Thx for the fast reply, but that doesnt exactly give me any help at all... Im still back at square 1 with that example mate...

:(
Dec 22 '07 #3
SammyB
807 Expert 512MB
Say I have a string with a length of 30 chars, and I want to re-format that string into lines with each a maximum of 10 chars.
How do I format that text without changing the sentences involved in that string?
I mean, I want the text to lined up as I mentioned earlier - but I dont want the text to be messed up when the formatting is done....

Is there a way todo this?

/Regards, Geir G.
I'm on Christmas break without a computer that has C#, so I may not be that great a help. If you are wanting to display the text formatted, then use a richtextbox instead of a textbox. If you really want exactly what you stated, then you would need to make a class, say SplitText. The constructor for SplitText would have the text and the max length. The properties would be Text(read/write) and FormattedText(read/only). HTH --Sam
Dec 22 '07 #4
I'm on Christmas break without a computer that has C#, so I may not be that great a help. If you are wanting to display the text formatted, then use a richtextbox instead of a textbox. If you really want exactly what you stated, then you would need to make a class, say SplitText. The constructor for SplitText would have the text and the max length. The properties would be Text(read/write) and FormattedText(read/only). HTH --Sam
Thx mate, its much appreciated - but Im still lost you know, some parts are just so d**n hard to understand sometimes. And dont take me wrong, I know how to code and etc in C# - just some parts doesnt come as easy as everything else... Hehehe, anyways I think a loop is needed for my operation to be truly working - so yeah, I would need a class and etc for the job.

/Regards, Geir G.
Dec 22 '07 #5
SammyB
807 Expert 512MB
> still lost
I know what you mean: I feel the same way in my Java class

> I think a loop is needed
That's correct! Just increment your loop by tens and grab each piece. If you want to be efficient, you might want to use a StringBuilder object to construct your formatted string, but if that's confusing, a string object will also work. You can post your code and we will help.
Dec 22 '07 #6
Expand|Select|Wrap|Line Numbers
  1.         private string FixString(string input)
  2.         {
  3.             string[] peek = input.Split(new char[] { ' ' });
  4.             string geek = null;
  5.             string temp = null;
  6.  
  7.             foreach (string gay in peek)
  8.             {
  9.                 temp += gay + " ";
  10.                 if (temp.Length > 10)
  11.                 {
  12.                     geek += temp + Environment.NewLine;
  13.                 }
  14.             }
  15.             return geek;
  16.         }
  17.  
Now, I have "solved" it so far - but still the most important thing is missing, how do I get it todo exactly as I asked for?

/Regards, Geir G.
Dec 22 '07 #7
SammyB
807 Expert 512MB
That's a good start, but in line 9, why do you add in a space? Do you want the output to have a space between each character? Also, does the routine work for a long string?
Dec 23 '07 #8
Expand|Select|Wrap|Line Numbers
  1.             string[] str = RemoveSpace(input);
  2.             int number = str.Length;
  3.             int count = 0;
  4.  
  5.             int split_at_chars = 43;
  6.             int debugmode = 1; /* 1 = on, 0 = off */
  7.  
  8.             string newtext = null;
  9.             split_at_chars--;
  10.  
  11.             foreach (string x in str)
  12.             {
  13.                 string substr = x;
  14.                 /* remove any newline */
  15.                 substr = substr.Replace("\r\n", "");
  16.                 substr = substr.Replace("\r", "");
  17.                 substr = substr.Replace("\n", "");
  18.  
  19.                 if (count <= split_at_chars)
  20.                 {
  21.                     if ((count + substr.Length) <= split_at_chars)
  22.                     {
  23.                         if (count == 0)
  24.                         {
  25.                             newtext += "            Û  Û  " + substr.ToString();
  26.                         }
  27.                         else
  28.                         {
  29.                             newtext += " " + substr.ToString();
  30.                         }
  31.                         count += (substr.Length + 1);
  32.                     }
  33.                     else
  34.                     {
  35.                         if (debugmode == 1)
  36.                         {
  37.                             newtext += "\r\n";
  38.                         }
  39.                         else
  40.                         {
  41.                             newtext += "\r\n";
  42.                         }
  43.  
  44.                         count = 0;
  45.  
  46.                         newtext += "            Û  Û  " + substr.ToString();
  47.                         count += substr.Length;
  48.                     }
  49.                 }
  50.                 else
  51.                 {
  52.                     if (debugmode == 1)
  53.                     {
  54.                         newtext += "\r\n";
  55.                     }
  56.                     else
  57.                     {
  58.                         newtext += "\r\n";
  59.                     }
  60.  
  61.                     count = 0;
  62.  
  63.                     newtext += "            Û  Û  " + substr.ToString();
  64.                     count += substr.Length;
  65.                 }
  66.             }
  67.             return newtext;
  68.  
There we go, finally - but now I have another problem, anyone care to adjust the code so it will automatically write exactly 43 chars per line anyways? I mean, so it adds an empty space where its missing a char to reach the 43 chars limit...

/Thanks in advance, Geir G.
Dec 28 '07 #9
CyberSoftHari
487 Expert 256MB
You are removing all space and new line string then where you want to give space?
Check your code:
Expand|Select|Wrap|Line Numbers
  1. /*Hari : Already removing space and storing in string array (which means string of each indes is a new line.*/
  2. string[] str = RemoveSpace(input);
  3.  
  4. foreach (string x in str)
  5.             {
  6.                 string substr = x;
  7.                 /* remove any newline */
  8.                 substr = substr.Replace("\r\n", "");
  9.                 substr = substr.Replace("\r", "");
  10.                 substr = substr.Replace("\n", "");
  11.     /*Hari : Here what is the use of this string replace     ?*/
Note: If you remove all space and new line then you can easily display 43 character using.
Expand|Select|Wrap|Line Numbers
  1. str.Substring(startIndex, Length);
Dec 29 '07 #10
After searching for your problem i have the below link which will help you .
StringFormatting

-thanks
52
Dec 29 '07 #11
CyberSoftHari
487 Expert 256MB
I would like to say String.Format is entirely differ from what he/she looking for and you cannot get link for anyone’s homework it is not good practice as well.
Dec 29 '07 #12
SammyB
807 Expert 512MB
You keep changing the problem and solving it incorrectly. This says to me that you do not understand the problem. Sit down. Turn off the rock music. Forget the code that you have written. Now restate the problem in two sentences. Then expand this description to include all of the details, but do not write any code. Now, think about what sort of object you need. What does it look like to the people that use it? What does it need to do internally. Now, write the smallest amount of code that you can to get some of these properties to work. Then, write a test driver to see if you can create this object and display its properties. Then go back to the original problem and see how this object fits and fill in the details. --Sam
Dec 29 '07 #13

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Thomas Philips | last post by:
Consider the following simple dictionary e={1:'one', 2: 'two'} e >>>'one' However, If I attempt to print e using a formatted string print " %(1)s" %e, I get a KeyError: '1'
10
by: Oliver S. | last post by:
I've developed a string-class that holds the string in an array which is a member-variable of the class and that has maximum-size which is con- figurable through a template-parameter. If any...
20
by: hagai26 | last post by:
I am looking for the best and efficient way to replace the first word in a str, like this: "aa to become" -> "/aa/ to become" I know I can use spilt and than join them but I can also use regular...
7
by: ilona | last post by:
Hi all, I store phone numbers in the database as 123447775665554(input mask is used for input, and some numbers have extensions), and I also know from db if the number is Canadian, US, or some...
3
by: Dominique Vandensteen | last post by:
after the very small & vs string.format discussion I did some speed tests... loop of 1.000.000 concatenations of 5 public string variables in a class gave following results: result = a & b...
4
by: Lauren Wilson | last post by:
Hi folks, We have a need to replace sub strings in certain message text. We use the Office Assistant to display help and often use the imbedded formatting commands. Those of you who have used...
15
by: angellian | last post by:
Sorry to raise a stupid question but I tried many methods which did work. how can I conserve the initial zero when I try to convert STR(06) into string in SQL statment? It always gives me 6...
7
by: L. Scott M. | last post by:
Have a quick simple question: dim x as string x = "1234567890" ------------------------------------------------------- VB 6 dim y as string
14
by: Scott M. | last post by:
Ok, this is driving me nuts... I am using VS.NET 2003 and trying to take an item out of a row in a loosely-typed dataset and place it in a label as a currency. As it is now, I am getting my...
2
by: Brian Parker | last post by:
I am beginning to work with VB2005.NET and I'm getting some problems with string formatting converting an application from VB6. VB6 code:- sTradeDate = Format(pArray(4,i Record), "mmddyy") ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.