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 Manipulations – Stripping a string

String Manipulations – Stripping a string

Scheduled Pinned Locked Moved C#
questioncsharpdatabasebeta-testinghelp
7 Posts 4 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
    Ryno Burger
    wrote on last edited by
    #1

    Hi fellow C#-ers I have a question, hope there's someone out there that might give me a possible solution to this issue. Basically I'm trying to parse a string (the query string) to my returnResults method which in turn returns a DataSet object. What I'm trying to achieve is to strip down the query string and work with a certain number of character which in turn will be parsed to my SqlDataAdapter to provide it with the table name. So, let’s say my query string looks like this: SELECT * FROM Product All I basically need is the table name, in this case "Product". How is it possible to only strip the string of the character "Product" ? public DataSet returnResults(string query) { SqlDataAdapter sqlDA = null; DataSet ds = null; StringBuilder str = new StringBuilder(); string tableName = null; str.Insert(0, query); tableName = str. //sqlDA.Fill(ds, ""); return ds; } Your feedback will be much appreciated. Thanks! R

    C L B 3 Replies Last reply
    0
    • R Ryno Burger

      Hi fellow C#-ers I have a question, hope there's someone out there that might give me a possible solution to this issue. Basically I'm trying to parse a string (the query string) to my returnResults method which in turn returns a DataSet object. What I'm trying to achieve is to strip down the query string and work with a certain number of character which in turn will be parsed to my SqlDataAdapter to provide it with the table name. So, let’s say my query string looks like this: SELECT * FROM Product All I basically need is the table name, in this case "Product". How is it possible to only strip the string of the character "Product" ? public DataSet returnResults(string query) { SqlDataAdapter sqlDA = null; DataSet ds = null; StringBuilder str = new StringBuilder(); string tableName = null; str.Insert(0, query); tableName = str. //sqlDA.Fill(ds, ""); return ds; } Your feedback will be much appreciated. Thanks! R

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      You can use a regex that looks for the word 'FROM' and then grabs the word after it.  You could even find that with string mashing It's a shame your code is dealing with SQL this directly

      Christian Graus - C++ MVP 'Why don't we jump on a fad that hasn't already been widely discredited ?' - Dilbert

      R 1 Reply Last reply
      0
      • R Ryno Burger

        Hi fellow C#-ers I have a question, hope there's someone out there that might give me a possible solution to this issue. Basically I'm trying to parse a string (the query string) to my returnResults method which in turn returns a DataSet object. What I'm trying to achieve is to strip down the query string and work with a certain number of character which in turn will be parsed to my SqlDataAdapter to provide it with the table name. So, let’s say my query string looks like this: SELECT * FROM Product All I basically need is the table name, in this case "Product". How is it possible to only strip the string of the character "Product" ? public DataSet returnResults(string query) { SqlDataAdapter sqlDA = null; DataSet ds = null; StringBuilder str = new StringBuilder(); string tableName = null; str.Insert(0, query); tableName = str. //sqlDA.Fill(ds, ""); return ds; } Your feedback will be much appreciated. Thanks! R

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

        Hi, there are basically two ways: 1) string operations there is String.IndexOf(str) to locate a substring, String.Substring(start, length) to take a substring, String.Trim() to remove white space at start and end, and String.Split(separator, times) to split a string by a separator In your case, I would try something like:

        int i=s.IndexOf(" FROM ");
        s=s.Substring(i+5); // s now starts right after FROM
        string[] sa=s.Split(' ', 2); // split once, at the first space
        s=sa[0].Trim(); // first word following FROM

        alternatively you could split first, then search which array element contains FROM and use the next array element. Or just assume it always must be the fourth element... You should carefully choose your startegy to locate the stuff you need. 2) regular expressions very powerful, not very intuitive; in this case I would prefer string operations :)

        Luc Pattyn

        R 1 Reply Last reply
        0
        • R Ryno Burger

          Hi fellow C#-ers I have a question, hope there's someone out there that might give me a possible solution to this issue. Basically I'm trying to parse a string (the query string) to my returnResults method which in turn returns a DataSet object. What I'm trying to achieve is to strip down the query string and work with a certain number of character which in turn will be parsed to my SqlDataAdapter to provide it with the table name. So, let’s say my query string looks like this: SELECT * FROM Product All I basically need is the table name, in this case "Product". How is it possible to only strip the string of the character "Product" ? public DataSet returnResults(string query) { SqlDataAdapter sqlDA = null; DataSet ds = null; StringBuilder str = new StringBuilder(); string tableName = null; str.Insert(0, query); tableName = str. //sqlDA.Fill(ds, ""); return ds; } Your feedback will be much appreciated. Thanks! R

          B Offline
          B Offline
          Bassam Saoud
          wrote on last edited by
          #4

          watch out from sql injection[^]

          1 Reply Last reply
          0
          • C Christian Graus

            You can use a regex that looks for the word 'FROM' and then grabs the word after it.  You could even find that with string mashing It's a shame your code is dealing with SQL this directly

            Christian Graus - C++ MVP 'Why don't we jump on a fad that hasn't already been widely discredited ?' - Dilbert

            R Offline
            R Offline
            Ryno Burger
            wrote on last edited by
            #5

            Hi Chistian, Thank you for your response. I also would like to have your ideas when you said

            Christian Graus wrote:

            It's a shame your code is dealing with SQL this directly

            What are your recommendations? I'm open and flexible to advice and ideas. Thanks. R

            C 1 Reply Last reply
            0
            • L Luc Pattyn

              Hi, there are basically two ways: 1) string operations there is String.IndexOf(str) to locate a substring, String.Substring(start, length) to take a substring, String.Trim() to remove white space at start and end, and String.Split(separator, times) to split a string by a separator In your case, I would try something like:

              int i=s.IndexOf(" FROM ");
              s=s.Substring(i+5); // s now starts right after FROM
              string[] sa=s.Split(' ', 2); // split once, at the first space
              s=sa[0].Trim(); // first word following FROM

              alternatively you could split first, then search which array element contains FROM and use the next array element. Or just assume it always must be the fourth element... You should carefully choose your startegy to locate the stuff you need. 2) regular expressions very powerful, not very intuitive; in this case I would prefer string operations :)

              Luc Pattyn

              R Offline
              R Offline
              Ryno Burger
              wrote on last edited by
              #6

              Hi Luc, Thank you for providing me with a solution. It makes sense now! I have to be honest, I haven’t had to deal with string manipulation in a long time. I appreciate the feedback. Thanks. R.

              1 Reply Last reply
              0
              • R Ryno Burger

                Hi Chistian, Thank you for your response. I also would like to have your ideas when you said

                Christian Graus wrote:

                It's a shame your code is dealing with SQL this directly

                What are your recommendations? I'm open and flexible to advice and ideas. Thanks. R

                C Offline
                C Offline
                Christian Graus
                wrote on last edited by
                #7

                I always like to see the data layer confined to stored procedures.

                Christian Graus - C++ MVP 'Why don't we jump on a fad that hasn't already been widely discredited ?' - Dilbert

                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