Replace function doesn't work properly 
Author Message
 Replace function doesn't work properly

Hi All,

I use the Replace function in a vb project but found out that it ain't
working properly.
I try to remove a space from a string using:

sStr = Replace(sStr, " ", "",1, -1)

I tried this, and with Chr(32) and also vbBinaryCompare.
The space stays in.

Any ideas what causes this?

Regards
Marco



Fri, 08 Apr 2011 17:57:32 GMT  
 Replace function doesn't work properly
Hi Marco,

First make sure it is actually Chr(32) you are trying to replace.  There are
other whitespace characters in the unicode range.  You can check the value
of a character using the Asc function along with the Mid function to
retrieve the character.


Quote:
> Hi All,

> I use the Replace function in a vb project but found out that it ain't
> working properly.
> I try to remove a space from a string using:

> sStr = Replace(sStr, " ", "",1, -1)

> I tried this, and with Chr(32) and also vbBinaryCompare.
> The space stays in.

> Any ideas what causes this?

> Regards
> Marco



Fri, 08 Apr 2011 18:07:17 GMT  
 Replace function doesn't work properly

on Mon, 20 Oct 2008 02:57:32 -0700 (PDT) bearing the
following fruit:

Quote:
>Hi All,

>I use the Replace function in a vb project but found out that it ain't
>working properly.
>I try to remove a space from a string using:

>sStr = Replace(sStr, " ", "",1, -1)

>I tried this, and with Chr(32) and also vbBinaryCompare.
>The space stays in.

>Any ideas what causes this?

Replace absolutely does work. The character you think is a
space probably isn't.

--
Jan Hyde (VB MVP)

https://mvp.support.microsoft.com/profile/Jan.Hyde



Fri, 08 Apr 2011 20:06:46 GMT  
 Replace function doesn't work properly

Quote:
> I use the Replace function in a vb project but found out that it ain't
> working properly.
> I try to remove a space from a string using:

> sStr = Replace(sStr, " ", "",1, -1)

> I tried this, and with Chr(32) and also vbBinaryCompare.
> The space stays in.

> Any ideas what causes this?

Chr$(160) instead of a normal space.


Fri, 08 Apr 2011 21:16:05 GMT  
 Replace function doesn't work properly

Quote:


> > I use the Replace function in a vb project but found out that it ain't
> > working properly.
> > I try to remove a space from a string using:

> > sStr = Replace(sStr, " ", "",1, -1)

> > I tried this, and with Chr(32) and also vbBinaryCompare.
> > The space stays in.

> > Any ideas what causes this?

> Chr$(160) instead of a normal space.

All,

I created a function to check all the characters in the string and it
definitely comes up with a Chr(32).
Still the Replace function won't remove it.
Does it have to do with the fact that I use this in VBA Word office
2003?

    sFinalName = Replace(sFinalName, Chr(46) & Chr(32) & Chr(46), ".",
1, -1, vbTextCompare)
    findchrs (sFinalName)

Public Function findchrs(str As String)

Dim i As Integer
Dim j As Integer
Dim sResult As String

i = 1
j = Len(str)
Do While i < j
   sResult = sResult & Asc(Mid(str, i, 1)) & ","
   i = i + 1
Loop
Debug.Print str
Debug.Print sResult

End Function



Sat, 09 Apr 2011 15:20:59 GMT  
 Replace function doesn't work properly
Hi Co,

In your code it is looking for the pattern ". ."
Does the output for sResult have 46,32,46 ?

I would suggest changing the code to check for AscW and see if that gives
something different.


Quote:



>> > I use the Replace function in a vb project but found out that it ain't
>> > working properly.
>> > I try to remove a space from a string using:

>> > sStr = Replace(sStr, " ", "",1, -1)

>> > I tried this, and with Chr(32) and also vbBinaryCompare.
>> > The space stays in.

>> > Any ideas what causes this?

>> Chr$(160) instead of a normal space.

> All,

> I created a function to check all the characters in the string and it
> definitely comes up with a Chr(32).
> Still the Replace function won't remove it.
> Does it have to do with the fact that I use this in VBA Word office
> 2003?

>    sFinalName = Replace(sFinalName, Chr(46) & Chr(32) & Chr(46), ".",
> 1, -1, vbTextCompare)
>    findchrs (sFinalName)

> Public Function findchrs(str As String)

> Dim i As Integer
> Dim j As Integer
> Dim sResult As String

> i = 1
> j = Len(str)
> Do While i < j
>   sResult = sResult & Asc(Mid(str, i, 1)) & ","
>   i = i + 1
> Loop
> Debug.Print str
> Debug.Print sResult

> End Function



Sat, 09 Apr 2011 15:31:06 GMT  
 Replace function doesn't work properly

Quote:




>>> I use the Replace function in a vb project but found out that it ain't
>>> working properly.
>>> I try to remove a space from a string using:
>>> sStr = Replace(sStr, " ", "",1, -1)
>>> I tried this, and with Chr(32) and also vbBinaryCompare.
>>> The space stays in.
>>> Any ideas what causes this?
>> Chr$(160) instead of a normal space.

> All,

> I created a function to check all the characters in the string and it
> definitely comes up with a Chr(32).
> Still the Replace function won't remove it.
> Does it have to do with the fact that I use this in VBA Word office
> 2003?

>     sFinalName = Replace(sFinalName, Chr(46) & Chr(32) & Chr(46), ".",
> 1, -1, vbTextCompare)
>     findchrs (sFinalName)

> Public Function findchrs(str As String)

> Dim i As Integer
> Dim j As Integer
> Dim sResult As String

> i = 1
> j = Len(str)
> Do While i < j
>    sResult = sResult & Asc(Mid(str, i, 1)) & ","
>    i = i + 1
> Loop
> Debug.Print str
> Debug.Print sResult

> End Function

Everything looks good in your code as far as I can see.  Can you cut & paste
the string you're using for sFinalName, just so we can all be 100% sure of
what's going on?  Or better yet, the output from findchrs?

Oh and to answer your question, while it's conceivable that the VBA in Word
2003 is different, it's rather unlikely, as VB6 and VBA are both ultimately
based on the same thing, with relatively minor variations for the most part,
and I'm 99.999% (add a few more 9's for good measure) sure that Replace
isn't one of them.  Therefore, it seems most likely that either there's a
problem with your source string, or there's a problem with the code that
nobody's seeing (like if you used a hard space in the code, as opposed to
the original string).

Rob



Sat, 09 Apr 2011 16:11:58 GMT  
 Replace function doesn't work properly

on Tue, 21 Oct 2008 00:20:59 -0700 (PDT) bearing the
following fruit:

Quote:



>> > I use the Replace function in a vb project but found out that it ain't
>> > working properly.
>> > I try to remove a space from a string using:

>> > sStr = Replace(sStr, " ", "",1, -1)

>> > I tried this, and with Chr(32) and also vbBinaryCompare.
>> > The space stays in.

>> > Any ideas what causes this?

>> Chr$(160) instead of a normal space.

>All,

>I created a function to check all the characters in the string and it
>definitely comes up with a Chr(32).
>Still the Replace function won't remove it.
>Does it have to do with the fact that I use this in VBA Word office
>2003?

Well that is a *VERY* important piece of information because
VB6 solutions won't always work in VBA.

However in this case it shouldn't make a difference.

Can you post the string here?

J

- Show quoted text -

Quote:

>    sFinalName = Replace(sFinalName, Chr(46) & Chr(32) & Chr(46), ".",
>1, -1, vbTextCompare)
>    findchrs (sFinalName)

>Public Function findchrs(str As String)

>Dim i As Integer
>Dim j As Integer
>Dim sResult As String

>i = 1
>j = Len(str)
>Do While i < j
>   sResult = sResult & Asc(Mid(str, i, 1)) & ","
>   i = i + 1
>Loop
>Debug.Print str
>Debug.Print sResult

>End Function

--
Jan Hyde (VB MVP)

https://mvp.support.microsoft.com/profile/Jan.Hyde



Sat, 09 Apr 2011 17:17:05 GMT  
 Replace function doesn't work properly

This is a perfect place for a For/Next loop:

Quote:
> Public Function findchrs(str As String)

> Dim i As Integer
> Dim sResult As String

For i = 1 To Len(str)

Quote:
>   sResult = sResult & Asc(Mid(str, i, 1)) & ","
Next
> Debug.Print str
> Debug.Print sResult

> End Function

There's nothing functionally wrong with what you had, but For/Next was made
for exactly trhis sort of thing.


Sat, 09 Apr 2011 21:30:39 GMT  
 Replace function doesn't work properly

Quote:
> Hi Co,

> In your code it is looking for the pattern ". ."
> Does the output for sResult have 46,32,46 ?

> I would suggest changing the code to check for AscW and see if that gives
> something different.






> >> > I use the Replace function in a vb project but found out that it ain't
> >> > working properly.
> >> > I try to remove a space from a string using:

> >> > sStr = Replace(sStr, " ", "",1, -1)

> >> > I tried this, and with Chr(32) and also vbBinaryCompare.
> >> > The space stays in.

> >> > Any ideas what causes this?

> >> Chr$(160) instead of a normal space.

> > All,

> > I created a function to check all the characters in the string and it
> > definitely comes up with a Chr(32).
> > Still the Replace function won't remove it.
> > Does it have to do with the fact that I use this in VBA Word office
> > 2003?

> > ? ?sFinalName = Replace(sFinalName, Chr(46) & Chr(32) & Chr(46), ".",
> > 1, -1, vbTextCompare)
> > ? ?findchrs (sFinalName)

> > Public Function findchrs(str As String)

> > Dim i As Integer
> > Dim j As Integer
> > Dim sResult As String

> > i = 1
> > j = Len(str)
> > Do While i < j
> > ? sResult = sResult & Asc(Mid(str, i, 1)) & ","
> > ? i = i + 1
> > Loop
> > Debug.Print str
> > Debug.Print sResult

> > End Function

Yes it does give me 46,32,46

I will post the results of the string tomorrow.

MArco



Sun, 10 Apr 2011 01:38:22 GMT  
 Replace function doesn't work properly

Quote:

> > Hi Co,

> > In your code it is looking for the pattern ". ."
> > Does the output for sResult have 46,32,46 ?

> > I would suggest changing the code to check for AscW and see if that gives
> > something different.






> > >> > I use the Replace function in a vb project but found out that it ain't
> > >> > working properly.
> > >> > I try to remove a space from a string using:

> > >> > sStr = Replace(sStr, " ", "",1, -1)

> > >> > I tried this, and with Chr(32) and also vbBinaryCompare.
> > >> > The space stays in.

> > >> > Any ideas what causes this?

> > >> Chr$(160) instead of a normal space.

> > > All,

> > > I created a function to check all the characters in the string and it
> > > definitely comes up with a Chr(32).
> > > Still the Replace function won't remove it.
> > > Does it have to do with the fact that I use this in VBA Word office
> > > 2003?

> > > ? ?sFinalName = Replace(sFinalName, Chr(46) & Chr(32) & Chr(46), ".",
> > > 1, -1, vbTextCompare)
> > > ? ?findchrs (sFinalName)

> > > Public Function findchrs(str As String)

> > > Dim i As Integer
> > > Dim j As Integer
> > > Dim sResult As String

> > > i = 1
> > > j = Len(str)
> > > Do While i < j
> > > ? sResult = sResult & Asc(Mid(str, i, 1)) & ","
> > > ? i = i + 1
> > > Loop
> > > Debug.Print str
> > > Debug.Print sResult

> > > End Function

> Yes it does give me 46,32,46

> I will post the results of the string tomorrow.

> MArco- Tekst uit oorspronkelijk bericht niet weergeven -

> - Tekst uit oorspronkelijk bericht weergeven -

The results of the test:

MILIEU.BODEMRICHTLIJN. .14.OKTOBER.2008.
77,73,76,73,69,85,46,66,79,68,69,77,82,73,67,72,84,76,73,74,78,46,32,46,49,52,46,79,75,84,79,66,69,82,46,50,48,48,56,

And still the space stays in there.

Marco



Mon, 11 Apr 2011 19:22:03 GMT  
 Replace function doesn't work properly
Hi Marco,


Quote:

>The results of the test:

>MILIEU.BODEMRICHTLIJN. .14.OKTOBER.2008.
>77,73,76,73,69,85,46,66,79,68,69,77,82,73,67,72,84,76,73,74,78,46,32,46,49,52,46,79,75,84,79,66,69,82,46,50,48,48,56,

>And still the space stays in there.

>Marco

Okay that looks like it is likely to be a unicode string not an ASCII
string.  Was that output using ASCW or ASC function ?

You need to be using AscW:

Public Function findchrs(str As String)
  Dim i As Integer
  Dim j As Integer
  Dim sResult As String
    i = 1
    j = Len(str)
    Do While i < j
       sResult = sResult & AscW(Mid(str, i, 1)) & ","
       i = i + 1
     Loop
    Debug.Print str
    Debug.Print sResult
End Function



Mon, 11 Apr 2011 19:36:38 GMT  
 Replace function doesn't work properly

Quote:
> Hi Marco,



> >The results of the test:

> >MILIEU.BODEMRICHTLIJN. .14.OKTOBER.2008.
> >77,73,76,73,69,85,46,66,79,68,69,77,82,73,67,72,84,76,73,74,78,46,32,46,49,52,46,79,75,84,79,66,69,82,46,50,48,48,56,

> >And still the space stays in there.

> >Marco

> Okay that looks like it is likely to be a unicode string not an ASCII
> string. ?Was that output using ASCW or ASC function ?

> You need to be using AscW:

> Public Function findchrs(str As String)
> ? Dim i As Integer
> ? Dim j As Integer
> ? Dim sResult As String
> ? ? i = 1
> ? ? j = Len(str)
> ? ? Do While i < j
> ? ? ? ?sResult = sResult & AscW(Mid(str, i, 1)) & ","
> ? ? ? ?i = i + 1
> ? ? ?Loop
> ? ? Debug.Print str
> ? ? Debug.Print sResult
> End Function

That was the output using Asc,
I didn't try the ASCW yet.

Marco



Mon, 11 Apr 2011 22:35:26 GMT  
 Replace function doesn't work properly

Quote:


>> Hi Marco,



>>> The results of the test:
>>> MILIEU.BODEMRICHTLIJN. .14.OKTOBER.2008.
>>> 77,73,76,73,69,85,46,66,79,68,69,77,82,73,67,72,84,76,73,74,78,46,32,46,49,52,46,79,75,84,79,66,69,82,46,50,48,48,56,
>>> And still the space stays in there.
>>> Marco
>> Okay that looks like it is likely to be a unicode string not an ASCII
>> string.  Was that output using ASCW or ASC function ?

>> You need to be using AscW:

>> Public Function findchrs(str As String)
>>   Dim i As Integer
>>   Dim j As Integer
>>   Dim sResult As String
>>     i = 1
>>     j = Len(str)
>>     Do While i < j
>>        sResult = sResult & AscW(Mid(str, i, 1)) & ","
>>        i = i + 1
>>      Loop
>>     Debug.Print str
>>     Debug.Print sResult
>> End Function

> That was the output using Asc,
> I didn't try the ASCW yet.

> Marco

Okay, I tried a replace on EXACTLY that string, even using the German
(Germany) regional settings and everything was fine for me.  So first, take
Bill's suggestion and use AscW to make absolutely certain that what you
think is a space (and a period) really is.

After that, also put another findchrs BEFORE your replace, just to make sure
you're working on the string you think you are.

Rob



Tue, 12 Apr 2011 04:05:40 GMT  
 Replace function doesn't work properly


Quote:


> >> Hi Marco,



> >>> The results of the test:
> >>> MILIEU.BODEMRICHTLIJN. .14.OKTOBER.2008.
> >>> 77,73,76,73,69,85,46,66,79,68,69,77,82,73,67,72,84,76,73,74,78,46,32,46,49,-52,46,79,75,84,79,66,69,82,46,50,48,48,56,
> >>> And still the space stays in there.
> >>> Marco
> >> Okay that looks like it is likely to be a unicode string not an ASCII
> >> string. ?Was that output using ASCW or ASC function ?

> >> You need to be using AscW:

> >> Public Function findchrs(str As String)
> >> ? Dim i As Integer
> >> ? Dim j As Integer
> >> ? Dim sResult As String
> >> ? ? i = 1
> >> ? ? j = Len(str)
> >> ? ? Do While i < j
> >> ? ? ? ?sResult = sResult & AscW(Mid(str, i, 1)) & ","
> >> ? ? ? ?i = i + 1
> >> ? ? ?Loop
> >> ? ? Debug.Print str
> >> ? ? Debug.Print sResult
> >> End Function

> > That was the output using Asc,
> > I didn't try the ASCW yet.

> > Marco

> Okay, I tried a replace on EXACTLY that string, even using the German
> (Germany) regional settings and everything was fine for me. ?So first, take
> Bill's suggestion and use AscW to make absolutely certain that what you
> think is a space (and a period) really is.

> After that, also put another findchrs BEFORE your replace, just to make sure
> you're working on the string you think you are.

> Rob- Tekst uit oorspronkelijk bericht niet weergeven -

> - Tekst uit oorspronkelijk bericht weergeven -

So I tried with AscW and got another result.
Where I get 32 with Asc I now got a 8192 with AscW.
Which I think is not even a normal character.
81 = Q and 91 = \.

Anyone an idea what this is?

Marco



Tue, 12 Apr 2011 16:01:20 GMT  
 
 [ 23 post ]  Go to page: [1] [2]

 Relevant Pages 

1. AllocUnit() function doesn't work properly with NTFS

2. VBScript Eval function doesn't work properly

3. CreateProcess function doesn't work properly.

4. CreateProcess function doesn't work properly.

5. Documenter doesn't work properly in A2K

6. Select method doesn't work properly

7. Small toolbar look - toolbar control doesn't work properly

8. STMAdmin.dll to query NT Log doesn't seem to work properly

9. My CommonDialog doesn't work properly

10. Match in DataCombo doesn't work properly

11. mmioInstallIOProc() doesn't work properly when compiled to native code

12. TVM_INSERTITEM doesn't seem to work properly

 

 
Powered by phpBB® Forum Software