Replacing character in a string

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • E11esar
    New Member
    • Nov 2008
    • 132

    Replacing character in a string

    Hi there.

    I am looping through a csv file and for each line, I need to replace a comma in a string that occurs between quotes.

    For example:

    "The", "cat", "sat", "on, the", "mat"

    Here I need to replace "on, the" with "on the".

    My first draft solution is to count the quotes and if odd, the next comma is rogue. My first draft solution is as follows:

    Code:
    int quotesCount = 0;
    for (int x = 0; x < line1.Length; x++)
    {
    if (line1[x] == (char)34) //quotes
    {
    quotesCount++;
    }
    if (line1[x] == (char)44) //comma
    {
    if ((quotesCount % 2 == 1) && (quotesCount > 1))
    {
    line1 = line1.Replace(line1[x], (char) 32); //replace comma with space character
    }
    }
    }
    This solution will replace all commas for a space character but I only want to replace the specific character for a given position value, here x.

    Does anybody know how I can do this please?

    Thank you.

    M :)
  • vekipeki
    Recognized Expert New Member
    • Nov 2007
    • 229

    #2
    String.Replace will replace all occurrences of a specified character with another character, so your line:
    Code:
    line1 = line1.Replace(line1[x], (char) 32);
    is doing exactly that, replacing all commas with a space.

    Since a String class is immutable (you cannot change it "in place"), consider using the StringBuilder Class (System.Text) and append character by character, while skipping the commas when needed.

    Comment

    • E11esar
      New Member
      • Nov 2008
      • 132

      #3
      Another solution

      Hi there.

      I found another way to do this:

      Code:
      char[] tmpBuffer = line1.ToCharArray();
      tmpBuffer[x] = (char)32;
      line1 = new string(tmpBuffer);
      Hopefully this may be of use to somebody else.

      Thank you.

      Mark :)

      Comment

      • Plater
        Recognized Expert Expert
        • Apr 2007
        • 7872

        #4
        Are you doing something special that the comma cannot be there?
        A comma inside a quote is part of the accepted CSV format.

        Otherwise I guess what you're doing is going to work the best (some people suggest regex, but I have never found a CSV regex that ACTUALLY worked correctly)

        Comment

        • vekipeki
          Recognized Expert New Member
          • Nov 2007
          • 229

          #5
          If your actual problem is to parse a csv file, then why do you remove the commas first?

          Just parse it on the fly, something like: find the opening quote, read all characters until you reach the closing quote (together with all the commas inside), then proceed to the next opening quote.

          Right now you are looping through the whole thing twice, first time to remove the commas, second time to parse it, and at the end you don't have the commas (which are, as Plater said, accepted when put inside quotes).

          Comment

          • E11esar
            New Member
            • Nov 2008
            • 132

            #6
            Originally posted by Plater
            Are you doing something special that the comma cannot be there?
            A comma inside a quote is part of the accepted CSV format.

            Otherwise I guess what you're doing is going to work the best (some people suggest regex, but I have never found a CSV regex that ACTUALLY worked correctly)
            Hi there. I need to use particular vales from within each line of the CSV file, so I have used a string.split() method so I can grasp the respective string array items and use them later to create the values in a sql WHERE clause.

            I am hence finding that the split command is also splitting within a string enclosed betwen quotes.

            Thank you.

            M :)

            Comment

            • E11esar
              New Member
              • Nov 2008
              • 132

              #7
              Originally posted by vekipeki
              If your actual problem is to parse a csv file, then why do you remove the commas first?

              Just parse it on the fly, something like: find the opening quote, read all characters until you reach the closing quote (together with all the commas inside), then proceed to the next opening quote.

              Right now you are looping through the whole thing twice, first time to remove the commas, second time to parse it, and at the end you don't have the commas (which are, as Plater said, accepted when put inside quotes).
              No at the moment I am splitting the file, by seperator equal to comma but that is not good, hence the commas are staying and I am removing the rogue commas using the method highlighted above. Hence just one pass is being made at a time.

              Your idea of reading it in by way of quotes could be a good idea though...

              Thank you.

              Mark :)

              Comment

              • vekipeki
                Recognized Expert New Member
                • Nov 2007
                • 229

                #8
                If it works, then it's great.

                Just note that
                Code:
                SELECT * FROM SomeTable
                WHERE SomeColumn='a b c'
                won't find columns where SomeColumn is 'a,b,c'.

                Comment

                • Plater
                  Recognized Expert Expert
                  • Apr 2007
                  • 7872

                  #9
                  If you plan to use SQL on it, have you considered opening the CSV file with oledb (using the Jet provider)?
                  that's what I do, it just reads it into a DataSet for me.

                  Comment

                  Working...