Re: Replace one occurence of string within string at certain position

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



RB Smissaert wrote:
Unless I am overlooking something simple again, I don't think that will
work. The thing is I need to replace at a specified position:

Sub test()
Dim str1 As String
Dim str2 As String

str1 = "123456789"

str2 = "XX"

str1 = Replace(str1, "4", str2, 4, 1)

'I need result to be "123XX56789"
'but will get "XX56789
MsgBox str1
End Sub

Well, I'm not sure what exactly you're overlooking, but when I do this in the
Immediate Window:

?Replace("123456789", "4", "XX", 4, 1)
123XX56789

It seems to do what you say you want it to? Maybe it's that I'm taking what you say
you need too literally, and what you really need is what you allude you need? <g>
IOW, you don't care what's at position four, and it may be entirely non-unique, but
you need to replace it? Yeah, in that case, it's a mess. You can split and
concatenate. Or you can slide stuff around with CopyMemory. Is this something you
need to do repeatedly?
--
..NET: It's About Trust!
http://vfred.mvps.org


.



Relevant Pages

  • Re: Replace one occurence of string within string at certain position
    ... The thing is I need to replace at a specified position: ... Dim str1 As String ... I would had swear that the VB6 function worked as your VB5 implementation ...
    (microsoft.public.vb.general.discussion)
  • RE: Find similar items from a two fields.
    ... Dim str1 As String ... Dim str2 As String ... I need macro which will compare two strings if they are similar. ...
    (microsoft.public.excel.programming)
  • RE: Find similar items from a two fields.
    ... Dim str1 As String ... Dim str2 As String ... I need macro which will compare two strings if they are similar. ...
    (microsoft.public.excel.programming)
  • Re: Need Help With Math Prolem Please
    ... dim aByte as Byte ... dim str1 as String = "" ... dim str2 as String = "" ...
    (microsoft.public.dotnet.languages.vb)
  • Re: variable size record length file parsing
    ... In fact, looking at your own code that is exactly what I would expect it to return because it concatenates *only* the lines it finds with the "00000001" in the specified position, instead of "all lines from that point onwards until a different value is discovered". ... Also, be wary of using CLng to convert parts of a string that might represent a specific value you are looking for, because it will return an error if it hits a string in which the text at that position cannot be legitimately converted into a Long. ... Dim f As Long, lSeq As Long ... Dim sLine As String, sTmp As String ...
    (microsoft.public.vb.general.discussion)