Re: Replace double spaces
Er, could we see a sample row or two of the file?
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)?
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:
Imports System.Text.RegularExpressions
Public Class Form1
Private Function SuperSplit(ByVal Source As String) As String()
Dim Tmp(0) As String
Const VBQuote As String = """"
Dim Patten As String = ",(?=(?:[^\" + VBQuote + "]*\" + VBQuote +
"[^\" + VBQuote + "]*\" + VBQuote + ")*(?![^\" + VBQuote + "]*\" + VBQuote + "))"
If String.IsNullOrEmpty(Source) Then
SuperSplit = Tmp
Else
'Split the string and return as array.
SuperSplit = Regex.Split(Source, Patten)
End If
End Function
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim s As String = """word1""" & "," & """word2""" & "," & """word3""" & "," & """word4"""
Dim v() As String = SuperSplit(s)
For Each Item As String In v
MessageBox.Show(Item, "", MessageBoxButtons.OK, MessageBoxIcon.Information)
Next Item
End Sub
End Class
Re: Replace double spaces
Quote:
Originally Posted by
Yumby
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(" ", "")
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!
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).
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.
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.
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.
Re: Replace double spaces
Quote:
Have you checked the content of datval?
I have now.
Quote:
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.
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 :)
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.