none
Add remove multiple space functionality to TextFieldParser RRS feed

  • Question

  • I'm new to programming so hopefully this makes some sense. I am using VB.net.

    When TextFieldParser has a delimiters set to {" "} and there are mulitple (and a varying number) spaces in a line:

    "fred   joe" (3 spaces between fred and joe in this case)

    ReadFields will return ("fred", " ", " ", "joe"). The TrimWhiteSpace property will not affect this because it only deals with leading and trailing white space.

    I would like to extend the functionality of TextFieldParser to be able to Reduce spaces between fields. I want to do this in a generalized way since I seem to run into this situation frequently. I have tried a few ways to do this and I am not really happy with them. My current one:

    - Read the file into an in memory string using a streamreader

    - process the string using a stringreader, looping through each line removing multiple spaces using an extension method I wrote to the string class based on a RegEx

    - create a new TextFieldParser and use ReadFields on the processed string

    This is not as clean or as general as I would like though. What I think I want to do is write a class that inherits TextFieldParser and adds a ReduceSpaces property. The constructor for the class has to call MyBase.new(file, stream, or textreader) as the first statement. I want to somehow preprocess the file, stream or textreader if ReduceSpaces is true. I'm not sure how to get access to the thing passed in, preprocess it and still have the TextFieldParser base class be able to access the preprocessed data. Is that possible?

    Am I headed in the wrong direction. Is there another way to do what I want?

    Monday, May 21, 2012 2:23 PM

Answers

  • Thanks for your help. I don't think regular expressions are what I am looking for in this generalized parser. Microsoft gave me almost all of what I need with TextFieldParser. I just have to figure out a way to remove the white space prior to using it. Maybe I just generalize my current approach as much as I can and live with it.
    Tuesday, May 22, 2012 2:28 PM

All replies

  • All you need to do is to make sure your constructor parameter list is different from the one default constructors in the class.  Like add another parameter to the standard constructor parameter list with  "Boolean ReducineSpaces".

    jdweng

    Monday, May 21, 2012 5:12 PM
  • I'm sorry but I am not following. If I understand your suggestion my constructor should look like this:

        Public Class MyTextFieldParser

            Inherits TextFieldParser

            Public Property ReduceWhiteSpace As Boolean

            Public Sub New(ByVal StreamName As Stream, ByVal ReduceWhiteSpace As Boolean)

                MyBase.New(StreamName)

                Me.ReduceWhiteSpace = ReduceWhiteSpace

            End Sub

            '... write some code to reduce the white space in StreamName

        End Class

    I'm not sure how this gives my class access to manipulate the data in StreamName and have the base class see those changes.

    Tuesday, May 22, 2012 12:34 PM
  • Change the parameter list

    From

    Public Sub New(ByVal StreamName As Stream, ByVal ReduceWhiteSpace As Boolean)

    To

    Public Sub New(ByRef StreamName As Stream, ByVal ReduceWhiteSpace As Boolean)


    jdweng

    Tuesday, May 22, 2012 12:40 PM
  • I don't think that will do the trick. The problem is that the constructor for TextFieldParser must take the StreamName ByVal. I wrote some code to test whether alterations to StreamName after it was passed to the TextFieldParser had any effect on the data TextFieldParser saw. It did not. So my assumption is that it took StreamName ByVal and I was operating on a different copy. What I would really like to do is preprocess the StreamName data and then invoke the TextFieldParser constructor but apparently that constructor call has to be the first thing in my constructor. Am I between a rock and a hard place on this approach?
    Tuesday, May 22, 2012 1:05 PM
  • try posting this question at the Regular expression website.  YOu may be able to use Regular expressions instead of the textfieldParser to get the results you are looking for.  I can never seem to get regular Expression to work for me, but it is worth a try.

    http://social.msdn.microsoft.com/Forums/en-US/regexp/threads


    jdweng

    Tuesday, May 22, 2012 1:40 PM
  • Thanks for your help. I don't think regular expressions are what I am looking for in this generalized parser. Microsoft gave me almost all of what I need with TextFieldParser. I just have to figure out a way to remove the white space prior to using it. Maybe I just generalize my current approach as much as I can and live with it.
    Tuesday, May 22, 2012 2:28 PM