-
Aug 25th, 2012, 07:43 AM
#1
Thread Starter
Lively Member
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
-
Aug 25th, 2012, 07:48 AM
#2
Re: Replace double spaces
Er, could we see a sample row or two of the file?
-
Aug 25th, 2012, 08:13 AM
#3
Thread Starter
Lively Member
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.
-
Aug 25th, 2012, 08:48 AM
#4
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
-
Aug 25th, 2012, 09:13 AM
#5
Re: Replace double spaces
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(" ", "")
-
Aug 25th, 2012, 11:04 AM
#6
Thread Starter
Lively Member
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!
-
Aug 25th, 2012, 11:24 AM
#7
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).
-
Aug 25th, 2012, 11:59 AM
#8
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.
-
Aug 25th, 2012, 12:05 PM
#9
Thread Starter
Lively Member
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.
-
Aug 25th, 2012, 12:11 PM
#10
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.
-
Aug 26th, 2012, 12:57 AM
#11
Thread Starter
Lively Member
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.
-
Aug 26th, 2012, 07:58 AM
#12
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
-
Aug 27th, 2012, 09:03 AM
#13
Thread Starter
Lively Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|