Avatar of Fordraiders
FordraidersFlag for United States of America

asked on 

Replace Exact word string procedure failing

vb.net 2003

Problem:
'   THIS FUNCTION WILL NOT CHANGE ANYTHING ?
when I call it


2 Tables:
Table 1
tblData
4 Fields
fldDId - Text   20
fldMfgname - Text 150
fldMfrnum - text  150
fldDescription - text 255
example:
0001  |LAWSON|12324R5|LAMP INCAND.  60WATTS
0002  |LAWSON|32DE45|1.A 2.B D.5 SPEEDBIT DRILLBIT
0003  |UNION BUTTEFIELD|435656| SOCKET HEAD CAP SCREW  1/2" X 3/4" LENGTH
0004  |LAWSON|12324R5|BULB , 100AMP
0005  |LAWSON|32DE45|DRILLBITS  3/4" X 6" LGTH

Table 2
tblCriteria
3 fields
fldCid
fldFindMe - text  150
fldReplaceWith -text 150
example data:
fldFindMe       fldReplaceWith
INCAND.        INCANDESCENT
BULB              LAMP

SO DATA BECOMES:
0001  |LAWSON|12324R5|LAMP INCANDESCENT  60WATTS '<---------- CHANGE HERE
0002  |LAWSON|32DE45|1.A 2.B D.5 SPEEDBIT DRILLBIT
0003  |UNION BUTTEFIELD|435656| SOCKET HEAD CAP SCREW  1/2" X 3/4" LENGTH
0004  |LAWSON|12324R5|LAMP , 100AMP  '<---------- CHANGE HERE
0005  |LAWSON|32DE45|DRILLBITS  3/4" X 6" LGTH


for Each drc As DataRow In tblCriteria.Rows
    For Each drd As DataRow In tblData.Rows
    drd("fldDescription") = FindAndReplace(drd("fldDescription"), drc("fldFindMe"), drc("fldReplaceWith"))
            Next
        Next

Private Function FindAndReplace(ByVal SearchText As String, ByVal Token As String, ByVal ReplaceText As String) As String
        Dim String1 As String
        Dim String2 As String
        Dim x As Integer

        For x = 0 To Len(SearchText)
            If x + Token.Length > SearchText.Length Then
                x = SearchText.Length - Token.Length
            End If
            If SearchText.Substring(x, Token.Length) = Token Then
                If x - 1 >= 0 Then
                    If IsNumeric(SearchText.Substring(x - 1, 1)) Then
                        String1 = SearchText.Substring(0, x)
                        String2 = SearchText.Substring(x + Len(Token))
                        Return String1 & ReplaceText & String2
                    End If
                End If
            End If

            If x + Token.Length = SearchText.Length Then Exit For
        Next
        Return SearchText
    End Function

Visual Basic.NET

Avatar of undefined
Last Comment
Fordraiders
Avatar of doraiswamy
doraiswamy
Flag of India image

See if this works for you. If you need any explanation, let me know

    Private Function FindAndReplace(ByVal SearchText As String, ByVal Token As String, ByVal ReplaceText As String) As String

        If SearchText.Contains(Token) Then
            Dim pos As Integer = InStr(SearchText, Token)
            Dim s1, s2 As String
            s1 = SearchText.Substring(0, pos - 1)
            s2 = SearchText.Substring(pos + Token.Length, SearchText.Length - pos - Token.Length)
            Return s1 & ReplaceText & s2
        End If
        Return SearchText
    End Function
Avatar of Sancler
Sancler

What's wrong with the "normal" String.Replace function?

   drd("fldDescription").Replace = drd("fldDescription").Replace( drc("fldFindMe"), drc("fldReplaceWith"))

Or, if that gives implicit type conversion problems

   Dim strOld As String = drd("fldDescription")
   Dim strFind As String = drc("fldFindMe")
   Dim strReplace As String = drc("fldReplaceWith")
   drd("fldDescription") = strOld.Replace(strFind, strReplace)

Roger
Avatar of Fordraiders
Fordraiders
Flag of United States of America image

ASKER

Roger I'am using the replace function now.
It seems to pick up words within other words...
I need to make sure. it searches for an extact string as the "fldFindme" token...
 
Avatar of Sancler
Sancler

It will replace the EXACT string.  Which, as I understood your original code, was what that was trying to do.

So this

   Dim strOld As String = "catapult, concatenate, big cat"
   Dim strFind As String = "cat"
   Dim strReplace As String = "dog"
   MsgBox(strOld.Replace(strFind, strReplace))

would produce

   "dogapult, condogenate, big dog"

Is that not what you want?

Roger
Avatar of Fordraiders
Fordraiders
Flag of United States of America image

ASKER

no no....
I do not want to find words within words..


If my token(fldFindme) is "cat" and replace is  "dog"
and the string is
"catapult, concatenate, big cat"

it should only replace "cat" with "dog"
"catapult, concatenate, big dog"

Thanks
Avatar of Fordraiders
Fordraiders
Flag of United States of America image

ASKER

dorias,
Private Function FindAndReplace(ByVal SearchText As String, ByVal Token As String, ByVal ReplaceText As String) As String

        If SearchText.Contains(Token) Then
            Dim pos As Integer = InStr(SearchText, Token)
            Dim s1, s2 As String
            s1 = SearchText.Substring(0, pos - 1)
            s2 = SearchText.Substring(pos + Token.Length, SearchText.Length - pos - Token.Length)
            Return s1 & ReplaceText & s2
        End If
        Return SearchText
    End Function

error .contains is not a member of   searchtext

Avatar of Sancler
Sancler

>>
If my token(fldFindme) is "cat" and replace is  "dog"
and the string is
"catapult, concatenate, big cat"

it should only replace "cat" with "dog"
"catapult, concatenate, big dog"
<<

Sounds easy ;-).  But how do you DEFINE the distinction between the situations in which you do replace the "cat" with "dog" and those in which you don't?  Is it space before and space after?  Or not (alpha character before or alpha character after)?  Or what?

It's beginning to look like one of your Regex questions ;-)

Roger
Avatar of Fordraiders
Fordraiders
Flag of United States of America image

ASKER

Is it space before and space after?
YES for now !
Avatar of Sancler
Sancler

If it were, then you could use this

   Dim strOld As String = "catapult, concatenate, big cat"
   Dim strFind As String = " " & "cat" & " "
   Dim strReplace As String = " " & "dog" & " "
   MsgBox(strOld.Replace(strFind, strReplace))

But that wouldn't replace cat in "big cat" because it doesn't have a space afterwards.  And, going back to to your real life example, BULB which you want replacing in this

0004  |LAWSON|12324R5|BULB , 100AMP

doesn't have a space in front of it.  So no, it is not "space before and space after".

The important part is the definition.  Once that is correct the coding is relatively simple - whether it involves Regex or not.

Roger
Avatar of Fordraiders
Fordraiders
Flag of United States of America image

ASKER

The important part is the definition ?
You mean me telling you what I need?
Looking for tokens
'================================
<space>CAT<space> <---  Token
If the word is at the end of the string ?

Avatar of Fordraiders
Fordraiders
Flag of United States of America image

ASKER

Is the function I posted not updateable ?


Avatar of Sancler
Sancler

>>
The important part is the definition ?
You mean me telling you what I need?
<<

No, that's not quite what I mean.  What I mean is YOU deciding precisely what you need.

Obviously, if someone else is to help you to achieve it, you must be able to describe it to that someone.  But the first step must be getting it clear in your own mind.

When I asked "But how do you DEFINE the distinction between the situations in which you do replace the "cat" with "dog" and those in which you don't?" you said

>>
Is it space before and space after?
YES for now !
<<

Either that does DEFINE what you want to do, or it doesn't.  It is pointless coding for one definition "for now" because when you change the definition the code will not work.  If it does DEFINE it, then I have explained how to adapt the original solution that I proposed to implement that.  But I also pointed out that, if that is indeed the DEFNITION of what you want to do, neither the "cat" example nor the examples in your original question were consistent with that DEFINITION.  The "cat" example wasn't because it didn't have a space afterwards: it was the end of the line.  The BULB example didn't because it didn't have a space before: it had "|" immediately in front of it.

You seem to be thinking that "words" are different from "strings".  So "cat" on its own is different from "cat" as part of "catapult" or "concatenate".  But in programming terms "cat" is the same String in all three situations.  So we need a way to tell the program when to treat it as a word (to be replaced) and when not to (and so leave it alone).  Saying "space before and space after" could be a perfectly valid definition, but it looks like it might not be enough to do that in this case.  What YOU need to decide is the DEFINITION, in these terms, of what you mean by a "word": the "rules" that distinguish what has to be replaced from what has to be left alone.

I, or someone else, might be able to GUESS from what has been said above that the DEFINITION of what you want to achieve is "replace this word with that word when and only when this word is preceded by space or "|" and succeeded by space or end of line.  But that would have to be a guess.  Only you can KNOW if those are the real rules and whether they are sufficient to cope with all the data.  So, for instance, nobody but you knows whether a "word" you want replacing might be preceded or succeeded by something else - like a comma or a dash - instead of space, "|" or end of line.  Your latest post asks

>>
Is the function I posted not updateable ?
<<

It already works.  But for it to change anything (a) the string to be replaced has to be preceded by a numeric character and (b) it will replace all such strings whether they are separate "words" or not.  That may, or may not, introduce another aspect of your DEFINITION of what, for these purposes, constitutes a "word" for replacement purposes.  But we have no way of knowing whether that is what is intended or, if it is, how those rules are supposed to be reconciled with other rules that we have been left to guess at.

My point is simply that, until we know exactly what that function is supposed to achieve, there is no way of knowing whether that function can be adapted to achieve that.  Or whether - even if it can - some other approach might be more efficient.

Roger
Avatar of Fordraiders
Fordraiders
Flag of United States of America image

ASKER

The function is to take "find" strings , if they are found replace them with the "replace" string
Find string            Replace string
cat                              feline                      
dog                             canine
roof                             shingle
bulb                             lamp
That are preceded and followed by a <space>cat<space>

The exception is the "find" string may be at the end of the "Input" string
which is the row record field in the database.

The input string:
the cat catapulted over the roof
I replaced the small bulb

The output would read:
the feline catapulted over the shingle
I replaced the small lamp

Thanks roger



 
Avatar of Sancler
Sancler

From your first post

>>
example:
[...]
0004  |LAWSON|12324R5|BULB , 100AMP
[...]
example data:
fldFindMe       fldReplaceWith
[...]
BULB              LAMP
[...]
SO DATA BECOMES:
[...]
0004  |LAWSON|12324R5|LAMP , 100AMP  '<---------- CHANGE HERE
<<

From your latest post

>>
The function is to take "find" strings , if they are found replace them with the "replace" string
[...]
That are preceded and followed by a <space>cat<space>

The exception is the "find" string may be at the end of the "Input" string
which is the row record field in the database.
<<

Using the rules in your latest post the replacement in the first post would not be made.

So was the example wrong, or are the rules wrong, or has something changed, or what?

Roger
Avatar of Fordraiders
Fordraiders
Flag of United States of America image

ASKER

example:
[...]
0004  |LAWSON|12324R5|BULB , 100AMP
[...]
example data:
fldFindMe       fldReplaceWith
[...]
BULB              LAMP
[...]
SO DATA BECOMES:
[...]
0004  |LAWSON|12324R5| LAMP , 100AMP  '<---------- CHANGE HERE
<<

The input strings will have a space in beginning position.
Access does  not allow spaces st the end of a field.
The example was posted wrong....
so ANY input string will have a space in the first position of the Input string.


Avatar of Sancler
Sancler

Try this

    Private Function ReplaceWords(ByVal oldText As String, ByVal oldWord As String, ByVal newWord As String) As String
        Dim newtext As String = ""
        Dim spaceAdded As Boolean = False
        'add space at end of string if necessary
        If oldText.LastIndexOf(" ") <> oldText.Length Then
            oldText &= " "
            spaceAdded = True
        End If
        'replace old word with new, including the leading and ...
        '... and trailing spaces which identify it as a WORD
        newtext = oldText.Replace(" " & oldWord & " ", " " & newWord & " ")
        'remove space if added at end of string
        If spaceAdded Then
            newtext = newtext.Substring(0, newtext.Length - 1)
        End If
        Return newtext
    End Function

It is, first, specific to the rules that you have set.  And, second, it strikes me as a case for which Regex would be appopriate: but I am not a Regex expert, which is why I have done it this way.

Roger
Avatar of Fordraiders
Fordraiders
Flag of United States of America image

ASKER

Robert, Your patience has been outstanding. I appreciate it very much !
bloody worth more than 500 points. The experience from your insight has been more valueable. I will test and let you know!
Cheers
ASKER CERTIFIED SOLUTION
Avatar of Sancler
Sancler

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of Fordraiders
Fordraiders
Flag of United States of America image

ASKER

Worked great !
Visual Basic.NET
Visual Basic.NET

Visual Basic .NET (VB.NET) is an object-oriented programming language implemented on the .NET framework, but also supported on other platforms such as Mono and Silverlight. Microsoft launched VB.NET as the successor to the Visual Basic language. Though it is similar in syntax to Visual Basic pre-2002, it is not the same technology,

96K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo