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.
  • R Offline
    R Offline
    ramdil
    wrote on last edited by
    #1

    Hi all. I have an application which will recieve ids from some other application. Now i need to store the incoming ids to a string builder, so that i can use it for later use.For this i have created a string builder as follows stringbuilder strIncomingEventID=new stringBuilder() and inside the receiving method i used the below stmts strIncomingEventID.Append(incomingmessage + Convert.ToChar(FIELD_SEP) ) now i want to retrieve each ids from it..for my manipulation... i am not getting split properties or any thing which is useful..can any one help me in this .One more thing is that i am appending a constant FIELD_SEP to end of incoming message when i store , so that when i want to retrieve the ids ,it will be useful..is it right method.Please advise me how to get the value from the string builder Thanks in advance

    Regards DilipRam

    M L M C V 5 Replies Last reply
    0
    • R ramdil

      Hi all. I have an application which will recieve ids from some other application. Now i need to store the incoming ids to a string builder, so that i can use it for later use.For this i have created a string builder as follows stringbuilder strIncomingEventID=new stringBuilder() and inside the receiving method i used the below stmts strIncomingEventID.Append(incomingmessage + Convert.ToChar(FIELD_SEP) ) now i want to retrieve each ids from it..for my manipulation... i am not getting split properties or any thing which is useful..can any one help me in this .One more thing is that i am appending a constant FIELD_SEP to end of incoming message when i store , so that when i want to retrieve the ids ,it will be useful..is it right method.Please advise me how to get the value from the string builder Thanks in advance

      Regards DilipRam

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

      Can't you store the IDs in a List<string> as they come in. YOu then have a list of IDs and can use this to build your string for storage at teh end.

      "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 all. I have an application which will recieve ids from some other application. Now i need to store the incoming ids to a string builder, so that i can use it for later use.For this i have created a string builder as follows stringbuilder strIncomingEventID=new stringBuilder() and inside the receiving method i used the below stmts strIncomingEventID.Append(incomingmessage + Convert.ToChar(FIELD_SEP) ) now i want to retrieve each ids from it..for my manipulation... i am not getting split properties or any thing which is useful..can any one help me in this .One more thing is that i am appending a constant FIELD_SEP to end of incoming message when i store , so that when i want to retrieve the ids ,it will be useful..is it right method.Please advise me how to get the value from the string builder Thanks in advance

        Regards DilipRam

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

        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] }


        G R 2 Replies Last reply
        0
        • R ramdil

          Hi all. I have an application which will recieve ids from some other application. Now i need to store the incoming ids to a string builder, so that i can use it for later use.For this i have created a string builder as follows stringbuilder strIncomingEventID=new stringBuilder() and inside the receiving method i used the below stmts strIncomingEventID.Append(incomingmessage + Convert.ToChar(FIELD_SEP) ) now i want to retrieve each ids from it..for my manipulation... i am not getting split properties or any thing which is useful..can any one help me in this .One more thing is that i am appending a constant FIELD_SEP to end of incoming message when i store , so that when i want to retrieve the ids ,it will be useful..is it right method.Please advise me how to get the value from the string builder Thanks in advance

          Regards DilipRam

          M Offline
          M Offline
          Manoj Kumar Rai
          wrote on last edited by
          #4

          Hi, The Sting class has a function "Split()", which returns array of String after spliting the String on the basis of a char.

          Manoj Never Gives up

          1 Reply Last reply
          0
          • 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] }


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

            Luc Pattyn wrote:

            Since your string operation is very simple (just a concatenation) there is no point in using StringBuilder.

            Actually, as it was done for every item that was recieved, using a StringBuilder is a good method. As he wants to process the separate items afterwards, putting them together as a single string is of course a bad idea, regardless of the method used. :)

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

            1 Reply Last reply
            0
            • R ramdil

              Hi all. I have an application which will recieve ids from some other application. Now i need to store the incoming ids to a string builder, so that i can use it for later use.For this i have created a string builder as follows stringbuilder strIncomingEventID=new stringBuilder() and inside the receiving method i used the below stmts strIncomingEventID.Append(incomingmessage + Convert.ToChar(FIELD_SEP) ) now i want to retrieve each ids from it..for my manipulation... i am not getting split properties or any thing which is useful..can any one help me in this .One more thing is that i am appending a constant FIELD_SEP to end of incoming message when i store , so that when i want to retrieve the ids ,it will be useful..is it right method.Please advise me how to get the value from the string builder Thanks in advance

              Regards DilipRam

              C Offline
              C Offline
              Chintan Desai
              wrote on last edited by
              #6

              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 1 Reply Last reply
              0
              • R ramdil

                Hi all. I have an application which will recieve ids from some other application. Now i need to store the incoming ids to a string builder, so that i can use it for later use.For this i have created a string builder as follows stringbuilder strIncomingEventID=new stringBuilder() and inside the receiving method i used the below stmts strIncomingEventID.Append(incomingmessage + Convert.ToChar(FIELD_SEP) ) now i want to retrieve each ids from it..for my manipulation... i am not getting split properties or any thing which is useful..can any one help me in this .One more thing is that i am appending a constant FIELD_SEP to end of incoming message when i store , so that when i want to retrieve the ids ,it will be useful..is it right method.Please advise me how to get the value from the string builder Thanks in advance

                Regards DilipRam

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

                string [] ids = strIncomingEventID.ToString().Split(...); that should do the trick... :-D

                V. If I don't see you in this world, I'll see you in the next one... And don't be late. (Jimi Hendrix)

                G R 2 Replies Last reply
                0
                • V V 0

                  string [] ids = strIncomingEventID.ToString().Split(...); that should do the trick... :-D

                  V. If I don't see you in this world, I'll see you in the next one... And don't be late. (Jimi Hendrix)

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

                  Yes, that would work, but it's a terrible work around. Concatenating the values into a big string, only to split it again... Why not encrypt the string, put it in an xml document, save the xml document to disk, load it from disk, extract the data, and decrypt it, while you're at it? ;) Chances are that the id:s aren't even strings at all, so a List<int> would be the best place to store them.

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

                  V T 2 Replies Last reply
                  0
                  • G Guffa

                    Yes, that would work, but it's a terrible work around. Concatenating the values into a big string, only to split it again... Why not encrypt the string, put it in an xml document, save the xml document to disk, load it from disk, extract the data, and decrypt it, while you're at it? ;) Chances are that the id:s aren't even strings at all, so a List<int> would be the best place to store them.

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

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

                    Guffa wrote:

                    Concatenating the values into a big string, only to split it again... Why not encrypt the string, put it in an xml document, save the xml document to disk, load it from disk, extract the data, and decrypt it, while you're at it?

                    Good thinking man, this is even better. :-) I merely replied to his actual problem ;p

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

                    1 Reply Last reply
                    0
                    • G Guffa

                      Yes, that would work, but it's a terrible work around. Concatenating the values into a big string, only to split it again... Why not encrypt the string, put it in an xml document, save the xml document to disk, load it from disk, extract the data, and decrypt it, while you're at it? ;) Chances are that the id:s aren't even strings at all, so a List<int> would be the best place to store them.

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

                      T Offline
                      T Offline
                      Talal Sultan
                      wrote on last edited by
                      #10

                      You forgot to insert the xml document in that SQL database and retrieve it :laugh:

                      "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." --Rich Cook

                      1 Reply Last reply
                      0
                      • V V 0

                        string [] ids = strIncomingEventID.ToString().Split(...); that should do the trick... :-D

                        V. If I don't see you in this world, I'll see you in the next one... And don't be late. (Jimi Hendrix)

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

                        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 M P G 4 Replies Last reply
                        0
                        • 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
                          • 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
                            #13

                            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

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

                              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
                              • 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
                                          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