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. String Builder

String Builder

Scheduled Pinned Locked Moved C#
helptutorial
21 Posts 9 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.
  • L Luc Pattyn

    A StringBuilder most of the time is used to improve the performance when a series of operations needs to be performed on textual data, that would otherwise be stored in a string (each operation would create a new string object and copy its data). Ultimately, when the assembly of the string is done, one normally converts the StringBuilder to a string using the ToString() method. Since your string operation is very simple (just a concatenation) there is no point in using StringBuilder. And as you figured, you need a string object in order to use the nice String methods such as Split(). [Correction: if you do this several times, there may be a performance gain in useing StringBuilder, but in the end you have to convert back to a string]. BTW: if FIELD_SEP is a constant, there is not much point in adding it permanenty; instead, I suggest you append it to the one string you are dealing with, when and where you need it. :) -- modified at 12:00 Friday 3rd August, 2007

    Luc Pattyn


    try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }


    R Offline
    R Offline
    ramdil
    wrote on last edited by
    #12

    Hi Thanks for the reply.The think here is that i will be having multiple strings coming from other application.It will be not single one or it can come any time and i need them for futher processing.So i am using string builder ,,but then how can i split these ids from the string builder.I hope i am clear abt my requirment

    Regards DilipRam

    L 1 Reply Last reply
    0
    • R ramdil

      Hi thanks for the reply,but the thing is that i will not be having single string value...there will be multiples values coming and i have to store it and so i how can use a string to store it.pls explain

      Regards DilipRam

      V Offline
      V Offline
      V 0
      wrote on last edited by
      #13

      If my answer did not satisfy your needs, please explain more clearly what you actually want.

      V.
      Stop smoking so you can: Enjoy longer the money you save. Moviereview Archive

      R 1 Reply Last reply
      0
      • C Chintan Desai

        why r u making it complicated.Instead use stack or queue class and store each items one by one in it.They r fast-efficient and easy to program.:rose:

        Regards Chintan www.visharadsoft.com (Nothing is so purify as KNOWLEDGE)

        R Offline
        R Offline
        ramdil
        wrote on last edited by
        #14

        Hi thanks for the reply But no idea on how to work with stacks and queues...

        Regards DilipRam

        M 1 Reply Last reply
        0
        • R ramdil

          Hi thanks for the reply,but the thing is that i will not be having single string value...there will be multiples values coming and i have to store it and so i how can use a string to store it.pls explain

          Regards DilipRam

          M Offline
          M Offline
          Malcolm Smart
          wrote on last edited by
          #15

          What's wrong with my first answer. As they values come in, add them to a container. A List would do. Then when they are all in, you can traverse the list doing what you want with them.

          "More functions should disregard input values and just return 12. It would make life easier." - comment posted on WTF

          1 Reply Last reply
          0
          • R ramdil

            Hi thanks for the reply But no idea on how to work with stacks and queues...

            Regards DilipRam

            M Offline
            M Offline
            Malcolm Smart
            wrote on last edited by
            #16

            Well, containers are the solution to your problem so learn how to use them. Containers are the solution to a lot of problems.

            "More functions should disregard input values and just return 12. It would make life easier." - comment posted on WTF

            1 Reply Last reply
            0
            • R ramdil

              Hi Thanks for the reply.The think here is that i will be having multiple strings coming from other application.It will be not single one or it can come any time and i need them for futher processing.So i am using string builder ,,but then how can i split these ids from the string builder.I hope i am clear abt my requirment

              Regards DilipRam

              L Offline
              L Offline
              Luc Pattyn
              wrote on last edited by
              #17

              ramdil wrote:

              how can i split these ids from the string builder

              As I said, use ToString() to turn StringBuilder into string, then String.Split() to split it. But for your situation, I would not use StringBuilder at all. Seams like you need some kind of stream or a named pipe, and perform WriteLine and ReadLine on it. :)

              Luc Pattyn


              try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }


              1 Reply Last reply
              0
              • V V 0

                If my answer did not satisfy your needs, please explain more clearly what you actually want.

                V.
                Stop smoking so you can: Enjoy longer the money you save. Moviereview Archive

                R Offline
                R Offline
                ramdil
                wrote on last edited by
                #18

                Hi thanks for the reply Actually i have an application which will recieve messages from some other application.It can happen at any time.Now i have a recieve method for this purpose..but i dont want to do any operations as soon as i recieve any id but i want it to be used later .So inorder to not loose the incoming id, i decided to used string builder so that i can append one by one the id's which are coming..but now if i want to extract it or split it,,i dont have any split method in string builder ,its only in string ..so what do i need to do. Hope i am clear.

                Regards DilipRam

                V 1 Reply Last reply
                0
                • R ramdil

                  Hi thanks for the reply,but the thing is that i will not be having single string value...there will be multiples values coming and i have to store it and so i how can use a string to store it.pls explain

                  Regards DilipRam

                  P Offline
                  P Offline
                  PIEBALDconsult
                  wrote on last edited by
                  #19

                  If you are saying that the values come in as something like: "a,b,c" "d,e,f" etc. Then split each incoming list of IDs as it arrives and use a collection to store the individual values.

                  1 Reply Last reply
                  0
                  • R ramdil

                    Hi thanks for the reply,but the thing is that i will not be having single string value...there will be multiples values coming and i have to store it and so i how can use a string to store it.pls explain

                    Regards DilipRam

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

                    ramdil wrote:

                    the thing is that i will not be having single string value...there will be multiples values coming and i have to store it and so i how can use a string to store it.

                    You don't store it in a single string. It's pointless to put the strings together, as you want them separate later on. Just use a List<string>. Add each string to the list, then when you have recieved them all, you can easily access all the strings. If the values are id:s, as the name suggests, you can use a List<int> instead. Why store them as strings, if they aren't?

                    --- single minded; short sighted; long gone;

                    1 Reply Last reply
                    0
                    • R ramdil

                      Hi thanks for the reply Actually i have an application which will recieve messages from some other application.It can happen at any time.Now i have a recieve method for this purpose..but i dont want to do any operations as soon as i recieve any id but i want it to be used later .So inorder to not loose the incoming id, i decided to used string builder so that i can append one by one the id's which are coming..but now if i want to extract it or split it,,i dont have any split method in string builder ,its only in string ..so what do i need to do. Hope i am clear.

                      Regards DilipRam

                      V Offline
                      V Offline
                      V 0
                      wrote on last edited by
                      #21

                      Then it is as I said: string [] ids = strIncomingEventID.ToString().Split(...); You can also do this if it is easier for you: string allids = strIncomingEventID.ToString(); string [] ids = allids.Split(...); The ToString() method of the StringBuilder class returns a variable with type string. If you're using Visual Studio, try putting a "." after ToString() and you'll see in intellisense that Split will come up in the list. (PS: for you info I put the "..." in Split because it is an overloaded method, view the signature on MSDN for more info on which arguments the Split method expects.) hope this helps.

                      V.
                      Stop smoking so you can: Enjoy longer the money you save. Moviereview Archive

                      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