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. Appending Quations to a string

Appending Quations to a string

Scheduled Pinned Locked Moved C#
help
23 Posts 7 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.
  • A Arun Philip Reynolds

    Hi Everyone, I am facing an issue in appending quotes to the string value that I am holding in a String Builder variable. The value that I hold in the variable is, abc@def@ghi@jkl@ I would like to change the @ with "," so that i get the final result as, "abc","def","ghi","jkl" I tried the string format, but did not succeed. Any help in this matter is greatly appriciated.

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

    Replace perhaps? But why create the string with the @s rather than the characters you want? Creating the string the way you want shouldn't be a problem. Perhaps you should explain what you want and show the code you have so far.

    A 1 Reply Last reply
    0
    • P PIEBALDconsult

      Replace perhaps? But why create the string with the @s rather than the characters you want? Creating the string the way you want shouldn't be a problem. Perhaps you should explain what you want and show the code you have so far.

      A Offline
      A Offline
      Arun Philip Reynolds
      wrote on last edited by
      #3

      try
      {

              //OleDbCommand oleDbCmd = new OleDbCommand("Select \* From \[Sheet1\]", oleDbCon);            
              //oleDbDa.SelectCommand = oleDbCmd;            
              oleDbDa.Fill(ds,"ServerName");
              DataTable dt = ds.Tables\["ServerName"\];
              //string str1stValue = ds.Tables\[0\].Rows\[0\]\[0\].ToString();
              foreach(DataRow dr in dt.Rows)
              {
                  foreach (DataColumn dc in dt.Columns)
                      strServerName.Append(dr\[dc\].ToString()).Append("\\",\\"");
                      //strServerName.AppendFormat("\\",\\"", dr\[dc\].ToString());
              }
             //strFinalList = strFinalList.AppendFormat("\\",\\"", strServerName);
              Response.Write(strServerName);
          }
      

      But here again i need to get rid of of the quotations at the end of the string and need to add it before the string. The result that is get is, abc","def","ghi","jkl"," As I had mentioned i'd like to get the result as, "abc","def","ghi","jkl"

      P L 2 Replies Last reply
      0
      • A Arun Philip Reynolds

        try
        {

                //OleDbCommand oleDbCmd = new OleDbCommand("Select \* From \[Sheet1\]", oleDbCon);            
                //oleDbDa.SelectCommand = oleDbCmd;            
                oleDbDa.Fill(ds,"ServerName");
                DataTable dt = ds.Tables\["ServerName"\];
                //string str1stValue = ds.Tables\[0\].Rows\[0\]\[0\].ToString();
                foreach(DataRow dr in dt.Rows)
                {
                    foreach (DataColumn dc in dt.Columns)
                        strServerName.Append(dr\[dc\].ToString()).Append("\\",\\"");
                        //strServerName.AppendFormat("\\",\\"", dr\[dc\].ToString());
                }
               //strFinalList = strFinalList.AppendFormat("\\",\\"", strServerName);
                Response.Write(strServerName);
            }
        

        But here again i need to get rid of of the quotations at the end of the string and need to add it before the string. The result that is get is, abc","def","ghi","jkl"," As I had mentioned i'd like to get the result as, "abc","def","ghi","jkl"

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

        How about:

        foreach(DataRow dr in dt.Rows)
        {
        foreach (DataColumn dc in dt.Columns)
        strServerName.AppendFormat( "\"{0}\"," , dr[dc] );
        }
        strServerName.Length-- ;

        A 1 Reply Last reply
        0
        • A Arun Philip Reynolds

          Hi Everyone, I am facing an issue in appending quotes to the string value that I am holding in a String Builder variable. The value that I hold in the variable is, abc@def@ghi@jkl@ I would like to change the @ with "," so that i get the final result as, "abc","def","ghi","jkl" I tried the string format, but did not succeed. Any help in this matter is greatly appriciated.

          W Offline
          W Offline
          wizardzz
          wrote on last edited by
          #5

          Based on your posts below, have you tried using string.Join?

          string example = String.Join(", ", string[] exList);

          "I have a theory that the truth is never told during the nine-to-five hours. " — Hunter S. Thompson

          1 Reply Last reply
          0
          • A Arun Philip Reynolds

            try
            {

                    //OleDbCommand oleDbCmd = new OleDbCommand("Select \* From \[Sheet1\]", oleDbCon);            
                    //oleDbDa.SelectCommand = oleDbCmd;            
                    oleDbDa.Fill(ds,"ServerName");
                    DataTable dt = ds.Tables\["ServerName"\];
                    //string str1stValue = ds.Tables\[0\].Rows\[0\]\[0\].ToString();
                    foreach(DataRow dr in dt.Rows)
                    {
                        foreach (DataColumn dc in dt.Columns)
                            strServerName.Append(dr\[dc\].ToString()).Append("\\",\\"");
                            //strServerName.AppendFormat("\\",\\"", dr\[dc\].ToString());
                    }
                   //strFinalList = strFinalList.AppendFormat("\\",\\"", strServerName);
                    Response.Write(strServerName);
                }
            

            But here again i need to get rid of of the quotations at the end of the string and need to add it before the string. The result that is get is, abc","def","ghi","jkl"," As I had mentioned i'd like to get the result as, "abc","def","ghi","jkl"

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

            What Piebald said; or else what I often do when I do not want the StringBuilder:

            string result="";
            string sep="";
            foreach(string s in strings) {
            result+=sep+"\""+s+"\"";
            sep=",";
            }

            :)

            Luc Pattyn [My Articles] Nil Volentibus Arduum

            P A B 3 Replies Last reply
            0
            • L Luc Pattyn

              What Piebald said; or else what I often do when I do not want the StringBuilder:

              string result="";
              string sep="";
              foreach(string s in strings) {
              result+=sep+"\""+s+"\"";
              sep=",";
              }

              :)

              Luc Pattyn [My Articles] Nil Volentibus Arduum

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

              I'd still use String.Format -- I find it easier to read than a bunch of concatenations.

              A L 2 Replies Last reply
              0
              • P PIEBALDconsult

                How about:

                foreach(DataRow dr in dt.Rows)
                {
                foreach (DataColumn dc in dt.Columns)
                strServerName.AppendFormat( "\"{0}\"," , dr[dc] );
                }
                strServerName.Length-- ;

                A Offline
                A Offline
                Arun Philip Reynolds
                wrote on last edited by
                #8

                Thanks PIEBALDconsult for the help. The solution is just perfect, but there is an extra comma at the end of the string, "abc","def","ghi","jkl", How could I get rid of the last comma?

                P 1 Reply Last reply
                0
                • P PIEBALDconsult

                  I'd still use String.Format -- I find it easier to read than a bunch of concatenations.

                  A Offline
                  A Offline
                  Arun Philip Reynolds
                  wrote on last edited by
                  #9

                  I'd like to go with PIEBALDconsult on this one, but thanks for the help :)

                  1 Reply Last reply
                  0
                  • P PIEBALDconsult

                    I'd still use String.Format -- I find it easier to read than a bunch of concatenations.

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

                    I use it sparingly, string.Format I mean. :)

                    Luc Pattyn [My Articles] Nil Volentibus Arduum

                    P 1 Reply Last reply
                    0
                    • A Arun Philip Reynolds

                      Thanks PIEBALDconsult for the help. The solution is just perfect, but there is an extra comma at the end of the string, "abc","def","ghi","jkl", How could I get rid of the last comma?

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

                      You probably left off the decrement of the Length.

                      A 1 Reply Last reply
                      0
                      • P PIEBALDconsult

                        You probably left off the decrement of the Length.

                        A Offline
                        A Offline
                        Arun Philip Reynolds
                        wrote on last edited by
                        #12

                        oops!!! I did miss that one out. After I included the decrement of the length, it is removing all the comma's from the string. I need the comma that is in between the strings but it should not add the comma at the end of the string. It is just like the SQL statement that we write. e.g. Select * from table where name like ('abc','def','ghi') I am trying to send the variable in this format to the stored procedure.

                        P 2 Replies Last reply
                        0
                        • A Arun Philip Reynolds

                          oops!!! I did miss that one out. After I included the decrement of the length, it is removing all the comma's from the string. I need the comma that is in between the strings but it should not add the comma at the end of the string. It is just like the SQL statement that we write. e.g. Select * from table where name like ('abc','def','ghi') I am trying to send the variable in this format to the stored procedure.

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

                          It should be outside the loop, as in the example I posted.

                          1 Reply Last reply
                          0
                          • A Arun Philip Reynolds

                            oops!!! I did miss that one out. After I included the decrement of the length, it is removing all the comma's from the string. I need the comma that is in between the strings but it should not add the comma at the end of the string. It is just like the SQL statement that we write. e.g. Select * from table where name like ('abc','def','ghi') I am trying to send the variable in this format to the stored procedure.

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

                            Arun Philip Reynolds wrote:

                            It is just like the SQL statement that we write.
                            e.g. Select * from table where name like ('abc','def','ghi')

                            If you are using SQL Server (and you should), then how about... SQL Server 2008 User Defined Table Types and Table-Valued Parameters[^]

                            A 1 Reply Last reply
                            0
                            • L Luc Pattyn

                              I use it sparingly, string.Format I mean. :)

                              Luc Pattyn [My Articles] Nil Volentibus Arduum

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

                              I use it whenever I can -- muaa haa ha ha! :cool:

                              1 Reply Last reply
                              0
                              • P PIEBALDconsult

                                Arun Philip Reynolds wrote:

                                It is just like the SQL statement that we write.
                                e.g. Select * from table where name like ('abc','def','ghi')

                                If you are using SQL Server (and you should), then how about... SQL Server 2008 User Defined Table Types and Table-Valued Parameters[^]

                                A Offline
                                A Offline
                                Arun Philip Reynolds
                                wrote on last edited by
                                #16

                                Thanks, but I just tried this,

                                strHolderString = strServerName.ToString();
                                strHolderString = strHolderString.TrimEnd(',');
                                strFinalList = new StringBuilder(strHolderString);

                                here the strHolderString is a string variable and I am getting the result that I was looking for. Thanks once again to everyone. :-D

                                P 1 Reply Last reply
                                0
                                • L Luc Pattyn

                                  What Piebald said; or else what I often do when I do not want the StringBuilder:

                                  string result="";
                                  string sep="";
                                  foreach(string s in strings) {
                                  result+=sep+"\""+s+"\"";
                                  sep=",";
                                  }

                                  :)

                                  Luc Pattyn [My Articles] Nil Volentibus Arduum

                                  A Offline
                                  A Offline
                                  Arun Philip Reynolds
                                  wrote on last edited by
                                  #17

                                  strHolderString = strServerName.ToString();
                                  strHolderString = strHolderString.TrimEnd(',');
                                  strFinalList = new StringBuilder(strHolderString);

                                  I just tried the above and it works for me. Here the strHolderString is a string variable and I used the TrimEnd function to get rid of the trailing comma. The strFinalList is a StringBuilder variable. Thanks once again to all

                                  1 Reply Last reply
                                  0
                                  • A Arun Philip Reynolds

                                    Thanks, but I just tried this,

                                    strHolderString = strServerName.ToString();
                                    strHolderString = strHolderString.TrimEnd(',');
                                    strFinalList = new StringBuilder(strHolderString);

                                    here the strHolderString is a string variable and I am getting the result that I was looking for. Thanks once again to everyone. :-D

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

                                    :sigh: Just decrement the Length. Why create two Strings and another StringBuilder?

                                    1 Reply Last reply
                                    0
                                    • A Arun Philip Reynolds

                                      Hi Everyone, I am facing an issue in appending quotes to the string value that I am holding in a String Builder variable. The value that I hold in the variable is, abc@def@ghi@jkl@ I would like to change the @ with "," so that i get the final result as, "abc","def","ghi","jkl" I tried the string format, but did not succeed. Any help in this matter is greatly appriciated.

                                      S Offline
                                      S Offline
                                      Subin Mavunkal
                                      wrote on last edited by
                                      #19

                                      You can use SmartPaster addin for visual studio.Copy the text you needed and paste it as string builder.Here is the code and installer http://www.martinwilley.com/blog/2010/06/06/SmartPasteIn2010.aspx[^]

                                      1 Reply Last reply
                                      0
                                      • L Luc Pattyn

                                        What Piebald said; or else what I often do when I do not want the StringBuilder:

                                        string result="";
                                        string sep="";
                                        foreach(string s in strings) {
                                        result+=sep+"\""+s+"\"";
                                        sep=",";
                                        }

                                        :)

                                        Luc Pattyn [My Articles] Nil Volentibus Arduum

                                        B Offline
                                        B Offline
                                        BobJanova
                                        wrote on last edited by
                                        #20

                                        There's pretty much no reason to avoid StringBuilder, it's in the core Framework and the memory footprint of creating a new object is likely to be less than all the wasted strings you make in a concatenation session.

                                        1 Reply Last reply
                                        0
                                        • A Arun Philip Reynolds

                                          Hi Everyone, I am facing an issue in appending quotes to the string value that I am holding in a String Builder variable. The value that I hold in the variable is, abc@def@ghi@jkl@ I would like to change the @ with "," so that i get the final result as, "abc","def","ghi","jkl" I tried the string format, but did not succeed. Any help in this matter is greatly appriciated.

                                          P Offline
                                          P Offline
                                          Pete OHanlon
                                          wrote on last edited by
                                          #21

                                          An alternative approach is to use a list of strings instead of a StringBuilder to build your list and then concatenate them as you need. Here's one way to do this:

                                          public class FormattedStrings : List<string>()
                                          {
                                          public override string ToString()
                                          {
                                          StringBuilder sb = new StringBuilder();
                                          bool started = false;
                                          foreach (string item in this)
                                          {
                                          if (started)
                                          {
                                          sb.AppendFormat(",\"{0}\"", item);
                                          }
                                          else
                                          {
                                          started = true;
                                          sb.AppendFormat("\"{0}\"", item);
                                          }
                                          }

                                          return sb.ToString();
                                          

                                          }
                                          }

                                          I am assuming that you have assigned the @ symbol when you built your StringBuilder.

                                          *pre-emptive celebratory nipple tassle jiggle* - Sean Ewington

                                          "Mind bleach! Send me mind bleach!" - Nagy Vilmos

                                          My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility

                                          P 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