Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. text split up!

text split up!

Scheduled Pinned Locked Moved C#
questionregexhelptutorial
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    nesfrank
    wrote on last edited by
    #1

    Guys, I send to a webservice a gift message string which can be any length. That service splits the string into 30 characters and creates a new line. example: I type: Hi Jocey!! I Hope you have a stupendous 30th birthday! I'm aorry I can't make it! Suexoxo The web service splits as follows: Hi Jocey!! I Hope you have a s tupendous 30th birthday! I'm s orry I can't make it! Suexoxo My question is, can we do something like this, if the break happens to be in a word like S orry can we fill up with spaces and send that sorry next line. I am unclear please follow up. may be some regular expression? Thank you very much for your help guys!

    E G 2 Replies Last reply
    0
    • N nesfrank

      Guys, I send to a webservice a gift message string which can be any length. That service splits the string into 30 characters and creates a new line. example: I type: Hi Jocey!! I Hope you have a stupendous 30th birthday! I'm aorry I can't make it! Suexoxo The web service splits as follows: Hi Jocey!! I Hope you have a s tupendous 30th birthday! I'm s orry I can't make it! Suexoxo My question is, can we do something like this, if the break happens to be in a word like S orry can we fill up with spaces and send that sorry next line. I am unclear please follow up. may be some regular expression? Thank you very much for your help guys!

      E Offline
      E Offline
      Ennis Ray Lynch Jr
      wrote on last edited by
      #2

      Yes you can. Find all instances of the space character in your long string. Then only send strings based on the space not based on the character position. You can then add padding to the end if necessary.

      Need software developed? Offering C# development all over the United States, ERL GLOBAL, Inc is the only call you will have to make.
      Happiness in intelligent people is the rarest thing I know. -- Ernest Hemingway
      Most of this sig is for Google, not ego.

      1 Reply Last reply
      0
      • N nesfrank

        Guys, I send to a webservice a gift message string which can be any length. That service splits the string into 30 characters and creates a new line. example: I type: Hi Jocey!! I Hope you have a stupendous 30th birthday! I'm aorry I can't make it! Suexoxo The web service splits as follows: Hi Jocey!! I Hope you have a s tupendous 30th birthday! I'm s orry I can't make it! Suexoxo My question is, can we do something like this, if the break happens to be in a word like S orry can we fill up with spaces and send that sorry next line. I am unclear please follow up. may be some regular expression? Thank you very much for your help guys!

        G Offline
        G Offline
        Guffa
        wrote on last edited by
        #3

        Use the LastIndexOf method to find the last space within the first 30 characters and split the string there. Use the PadRight method to add spaces. Example:

        string msg = "Hi Jocey!! I Hope you have a stupendous 30th birthday! I'm aorry I can't make it! Suexoxo";

        List lines = new List();
        while (msg.Length > 30) {
        int pos = msg.LastIndexOf(' ', 29, 30);
        if (pos != -1) {
        lines.Add(msg.Substring(0, pos).PadRight(30));
        msg = msg.Substring(pos + 1);
        } else {
        lines.Add(msg.Substring(0, 30));
        msg = msg.Substring(30);
        }
        }
        lines.Add(msg.PadRight(30));

        foreach (string s in lines) Console.WriteLine("\""+s+"\"");

        Despite everything, the person most likely to be fooling you next is yourself.

        N 1 Reply Last reply
        0
        • G Guffa

          Use the LastIndexOf method to find the last space within the first 30 characters and split the string there. Use the PadRight method to add spaces. Example:

          string msg = "Hi Jocey!! I Hope you have a stupendous 30th birthday! I'm aorry I can't make it! Suexoxo";

          List lines = new List();
          while (msg.Length > 30) {
          int pos = msg.LastIndexOf(' ', 29, 30);
          if (pos != -1) {
          lines.Add(msg.Substring(0, pos).PadRight(30));
          msg = msg.Substring(pos + 1);
          } else {
          lines.Add(msg.Substring(0, 30));
          msg = msg.Substring(30);
          }
          }
          lines.Add(msg.PadRight(30));

          foreach (string s in lines) Console.WriteLine("\""+s+"\"");

          Despite everything, the person most likely to be fooling you next is yourself.

          N Offline
          N Offline
          nesfrank
          wrote on last edited by
          #4

          your anser works like a miracle. one more question for you. how can I clean up the text from carriage returns and line feeds? before starting the process? please advice.

          G 1 Reply Last reply
          0
          • N nesfrank

            your anser works like a miracle. one more question for you. how can I clean up the text from carriage returns and line feeds? before starting the process? please advice.

            G Offline
            G Offline
            Guffa
            wrote on last edited by
            #5

            I suppose that you want spaces in place of the line breaks? msg = msg.Replace("\r\n", " ");

            Despite everything, the person most likely to be fooling you next is yourself.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups