Results 1 to 13 of 13

Thread: Replace double spaces

  1. #1

    Thread Starter
    Lively Member Yumby's Avatar
    Join Date
    Feb 2009
    Posts
    120

    Replace double spaces

    Hi there, I'm trying to identify and fix double spaces in a string (that comes from a csv file.) My code originally came from kleinma in this thread, but after tweeking it, it's not working. On the net, everyone else seems to be using the same indexof/replace code. So can someone tell me why my code isn't working? That would great. Thank you.

    Code:
    	While Not therea.EndOfData
                    therow = therea.ReadFields()
                    x = 0
                    For Each datval In therow
                        wordat(x) = datval
    
                        If datval.IndexOf("  ") <> -1 Then
                            datval = datval.Replace("  ", " ")
                            MsgBox("double space found")
                        End If
    
                    Next
                End While

  2. #2
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Replace double spaces

    Er, could we see a sample row or two of the file?

  3. #3

    Thread Starter
    Lively Member Yumby's Avatar
    Join Date
    Feb 2009
    Posts
    120

    Re: Replace double spaces

    I'm just using a test file for now....

    "word1", "word2", "word3 ", "word4"

    I just did a quick test. When I use...

    "word1", "wo rd2", "word3 ", "word4"

    It identifies "wo rd2" but it doesn't identify "word3 " (and maybe for that I have to use Ltrim or Rtrim or whatever it's called)?
    Last edited by Yumby; Aug 25th, 2012 at 08:18 AM.

  4. #4
    Fanatic Member BenJones's Avatar
    Join Date
    Mar 2010
    Location
    Wales UK
    Posts
    629

    Re: Replace double spaces

    try this duno if it help I was working on a csv reader and editor class and used this to split the quoted strings. give this a try.

    vbnet Code:
    1. Imports System.Text.RegularExpressions
    2. Public Class Form1
    3.  
    4.     Private Function SuperSplit(ByVal Source As String) As String()
    5.         Dim Tmp(0) As String
    6.         Const VBQuote As String = """"
    7.  
    8.         Dim Patten As String = ",(?=(?:[^\" + VBQuote + "]*\" + VBQuote +
    9.             "[^\" + VBQuote + "]*\" + VBQuote + ")*(?![^\" + VBQuote + "]*\" + VBQuote + "))"
    10.  
    11.         If String.IsNullOrEmpty(Source) Then
    12.             SuperSplit = Tmp
    13.         Else
    14.             'Split the string and return as array.
    15.             SuperSplit = Regex.Split(Source, Patten)
    16.         End If
    17.  
    18.     End Function
    19.  
    20.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    21.         Dim s As String = """word1""" & "," & """word2""" & "," & """word3""" & "," & """word4"""
    22.         Dim v() As String = SuperSplit(s)
    23.  
    24.         For Each Item As String In v
    25.             MessageBox.Show(Item, "", MessageBoxButtons.OK, MessageBoxIcon.Information)
    26.         Next Item
    27.     End Sub
    28. End Class

  5. #5
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Replace double spaces

    Quote Originally Posted by Yumby View Post
    I'm just using a test file for now....

    "word1", "word2", "word3 ", "word4"

    I just did a quick test. When I use...

    "word1", "wo rd2", "word3 ", "word4"

    It identifies "wo rd2" but it doesn't identify "word3 " (and maybe for that I have to use Ltrim or Rtrim or whatever it's called)?
    But there aren't any double spaces in that. The code you used is designed to reduce something like "My[SpaceSpace]apple" to "My[Space]apple". If you're trying to eliminate all spaces then you need to use ..

    datval = datval.Replace(" ", "")

  6. #6

    Thread Starter
    Lively Member Yumby's Avatar
    Join Date
    Feb 2009
    Posts
    120

    Re: Replace double spaces

    Okay thanks for the help. I'm getting there (slowly). Long story short, how do I identify double spaces before and after a word? I've searched the net but no luck. Something like.....

    If wordat(x) contains double spaces before the word then
    trim wordat(x)
    elseif wordat(x) contains double spaces after the word then
    trim wordat(x)
    end if

    Any tips, hints, code out there? BTW I'm not trying to delete all spaces. Thanks once again!

  7. #7
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Replace double spaces

    Well, the code you started with should work. Alternatively you can use the Boolean .Contains(substring) for the test instead of .IndexOf(substring).

  8. #8
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: Replace double spaces

    You should also probably use a loop to make sure you get all the double spaces. The code would work for a double space but would leave a double space if there happened to be a tripple space somewhere. A loop would would replace the resulting double space on the next pass if needed.

  9. #9

    Thread Starter
    Lively Member Yumby's Avatar
    Join Date
    Feb 2009
    Posts
    120

    Re: Replace double spaces

    Here's my current file/row....

    "word1", "wo rd2", "word3 ", " word4"

    And I tried this.....
    Code:
    If datval.Contains("  ") = True Then
    MsgBox("2 spaces in " & wordat(x))
    But it only identifies "wo rd2" (not "word3 " or " word4") so what am I doing wrong now?

    DataMiser - Thanks, I'll work on that after I sort out this problem.
    Last edited by Yumby; Aug 25th, 2012 at 12:12 PM.

  10. #10
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: Replace double spaces

    Have you checked the content of datval? Perhaps it is being trimed by your read routine. Whatever the case it sounds like there are not 2 spaces in the actual value you have in the var at this point.

  11. #11

    Thread Starter
    Lively Member Yumby's Avatar
    Join Date
    Feb 2009
    Posts
    120

    Re: Replace double spaces

    Have you checked the content of datval?
    I have now.
    Perhaps it is being trimed by your read routine.
    Yes this appears to be the problem. I didn't know the read routine trimmed it. I've haven't come across any information or even a warning about this on the net. So I'll keep looking and if you or anyone else can point me in the right direction, that would be great.

  12. #12
    Frenzied Member
    Join Date
    Jul 2011
    Location
    UK
    Posts
    1,335

    Re: Replace double spaces

    I believe you are using a TextFieldParser to read your file. The documents are here and you'll see it has a TrimWhiteSpace Property which by default is set to True.

    In your example, you are enclosing your fields in quotes so you might also like to check out the HasFieldsEnclosedInQuotes Property.

    Also, this forum's software automatically trims double spaces down to single spaces so there were no double spaces in your examples which probably confused a few people

  13. #13

    Thread Starter
    Lively Member Yumby's Avatar
    Join Date
    Feb 2009
    Posts
    120

    Re: Replace double spaces

    Thanks Inferrd. Yes I'm using a TextFieldParser. I've been looking in the wrong areas for an answer and getting a big nothing. Your info/links look like they're on the money. So I'll check them out and post what I find out/solve. As for the forum software automatically trimming down to a single space.... That's irony for you isn't it (sorry if I confused anyone!) Cheers.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width