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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • n00bish
    New Member
    • Dec 2007
    • 5

    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.
    Last edited by n00bish; Dec 22 '07, 09:56 AM. Reason: forgot to include system....
  • CyberSoftHari
    Recognized Expert Contributor
    • Sep 2007
    • 488

    #2
    Doing homework here is prohibited and you have to try your self. I can just point you.

    [CODE=cpp]String strVariable;
    String[] strArray;
    strArray = strVariable.Spl it(char[] seperator, int Count);[/CODE]

    Comment

    • n00bish
      New Member
      • Dec 2007
      • 5

      #3
      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...

      :(

      Comment

      • SammyB
        Recognized Expert Contributor
        • Mar 2007
        • 807

        #4
        Originally posted by n00bish
        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(r ead/only). HTH --Sam

        Comment

        • n00bish
          New Member
          • Dec 2007
          • 5

          #5
          Originally posted by SammyB
          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(r ead/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.

          Comment

          • SammyB
            Recognized Expert Contributor
            • Mar 2007
            • 807

            #6
            > 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.

            Comment

            • n00bish
              New Member
              • Dec 2007
              • 5

              #7
              Code:
                      private string FixString(string input)
                      {
                          string[] peek = input.Split(new char[] { ' ' });
                          string geek = null;
                          string temp = null;
              
                          foreach (string gay in peek)
                          {
                              temp += gay + " ";
                              if (temp.Length > 10)
                              {
                                  geek += temp + Environment.NewLine;
                              }
                          }
                          return geek;
                      }
              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.

              Comment

              • SammyB
                Recognized Expert Contributor
                • Mar 2007
                • 807

                #8
                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?

                Comment

                • n00bish
                  New Member
                  • Dec 2007
                  • 5

                  #9
                  Code:
                              string[] str = RemoveSpace(input);
                              int number = str.Length;
                              int count = 0;
                  
                              int split_at_chars = 43;
                              int debugmode = 1; /* 1 = on, 0 = off */
                  
                              string newtext = null;
                              split_at_chars--;
                  
                              foreach (string x in str)
                              {
                                  string substr = x;
                                  /* remove any newline */
                                  substr = substr.Replace("\r\n", "");
                                  substr = substr.Replace("\r", "");
                                  substr = substr.Replace("\n", "");
                  
                                  if (count <= split_at_chars)
                                  {
                                      if ((count + substr.Length) <= split_at_chars)
                                      {
                                          if (count == 0)
                                          {
                                              newtext += "            Û  Û  " + substr.ToString();
                                          }
                                          else
                                          {
                                              newtext += " " + substr.ToString();
                                          }
                                          count += (substr.Length + 1);
                                      }
                                      else
                                      {
                                          if (debugmode == 1)
                                          {
                                              newtext += "\r\n";
                                          }
                                          else
                                          {
                                              newtext += "\r\n";
                                          }
                  
                                          count = 0;
                  
                                          newtext += "            Û  Û  " + substr.ToString();
                                          count += substr.Length;
                                      }
                                  }
                                  else
                                  {
                                      if (debugmode == 1)
                                      {
                                          newtext += "\r\n";
                                      }
                                      else
                                      {
                                          newtext += "\r\n";
                                      }
                  
                                      count = 0;
                  
                                      newtext += "            Û  Û  " + substr.ToString();
                                      count += substr.Length;
                                  }
                              }
                              return newtext;
                  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.

                  Comment

                  • CyberSoftHari
                    Recognized Expert Contributor
                    • Sep 2007
                    • 488

                    #10
                    You are removing all space and new line string then where you want to give space?
                    Check your code:
                    [CODE=cpp]/*Hari : Already removing space and storing in string array (which means string of each indes is a new line.*/
                    string[] str = RemoveSpace(inp ut);

                    foreach (string x in str)
                    {
                    string substr = x;
                    /* remove any newline */
                    substr = substr.Replace( "\r\n", "");
                    substr = substr.Replace( "\r", "");
                    substr = substr.Replace( "\n", "");
                    /*Hari : Here what is the use of this string replace ?*/[/CODE]

                    Note: If you remove all space and new line then you can easily display 43 character using.
                    [CODE=cpp]str.Substring(s tartIndex, Length);[/CODE]

                    Comment

                    • alijannaty52
                      New Member
                      • Dec 2007
                      • 17

                      #11
                      After searching for your problem i have the below link which will help you .
                      StringFormattin g

                      -thanks
                      52

                      Comment

                      • CyberSoftHari
                        Recognized Expert Contributor
                        • Sep 2007
                        • 488

                        #12
                        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.

                        Comment

                        • SammyB
                          Recognized Expert Contributor
                          • Mar 2007
                          • 807

                          #13
                          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

                          Comment

                          Working...