Replace Fourth Character in a Word of 4 letters

Hi all!

How Can I replace the 4th letter, the last letter of a word composed of 4 letters?
This word always will change, Ex. PATO, COME, VENA, BADA, MENU, OLED, SANI

I need to replace this 4th letter with an “X”, like this:
Ex. PATX, COMX, VENX, BADX, MENX, OLEX, SANX.

I think in Replace, but It finds an specific word to replace.

This is the routine that I’m using:

'This routine deletes any prohibited vulgarity words on spanish,
'changing the last character of that word to X.
Dim RFC As String

Dim DeleteProhibited As String
Dim strPalabras As String

'Define all prohibited words.
strPalabras = “BUEIBUEYCACACACOCAGACAGOCAKACAKOCOGECOJA
strPalabras = strPalabras + “KOGEKOJOKAKAKULOMAMEMAMOMEAR*”
strPalabras = strPalabras + “MEASMEONMIONCOJECOJICOJOCULO*”
strPalabras = strPalabras + “FETOGUEYJOTOKACAKACOKAGAKAGO*”
strPalabras = strPalabras + “MOCOMULAPEDAPEDOPENEPUTAPUTO*”
strPalabras = strPalabras + “QULORATARUIN*”

'If anyone is found, change it.
If InStr(strPalabras, Left(strRFC, 4) + “*”) > 0 Then
'replace 4th letter of RFC to delete
'the prohibited word.
'Mid(strRFC, 4, 1) = “X”

End If

DeleteProhibited = strRFC

Return DeleteProhibited.

Any Ideas?

I’d suggest using a dictionary.

dim d as new dictionary

d.Value(“PATO”)=“PATX”
d.Value(“COME”)=“COMX”
etc.

newWord=d.Lookup(suspectWord, suspectWord)

This will return the “X” version of suspectWord if it exists, or suspectWord unchanged if it is not.

A lot of language filters are just replace filters, it’s generally easier to make them that way.
For example, this forum software is a replace and just changes the word to !@#$% for display.

Also, please post code inside [code] blocks so they’re formatted and easier to read.

[quote=228541:@Eric Williams]I’d suggest using a dictionary.

dim d as new dictionary

d.Value(“PATO”)=“PATX”
d.Value(“COME”)=“COMX”
etc.

newWord=d.Lookup(suspectWord, suspectWord)

This will return the “X” version of suspectWord if it exists, or suspectWord unchanged if it is not.[/quote]
I’ll gonna check it. Thanks

I would think this would be a good place for a regex solution. But for your current method,

DeleteProhibited=Mid(strRFC, 1, 3) + "X"

If the word is always 4 characters, use Left().

strRFC = Left(strRFC, 3) + "X"

An easy drop in solution:

  Dim BadWords() As String = split(strPalabras, "*")
  Dim Index As Integer = BadWords.IndexOf(strRFC)
  If Index > -1 Then DeleteProhibited = Left(BadWords(Index), 3) + "X"

I prefer a Dictionary approach similar to Eric’s though. Way easier and much faster if it’s a lot of text you constantly need to go through.
However, I would define the Dictionary once and store it as a property instead of filling it every time.

The you can do something like:

If BadWords.HashKey(strRFC) Then DeleteProhibited = BadWords.Value(rtfRFC)

[quote=228558:@Marco Hof]An easy drop in solution:

  Dim BadWords() As String = split(strPalabras, "*")
  Dim Index As Integer = BadWords.IndexOf(strRFC)
  If Index > -1 Then DeleteProhibited = Left(BadWords(Index), 3) + "X"

I prefer a Dictionary approach similar to Eric’s though. Way easier and much faster if it’s a lot of text you constantly need to go through.
However, I would define the Dictionary once and store it as a property instead of filling it every time.

The you can do something like:

If BadWords.HashKey(strRFC) Then DeleteProhibited = BadWords.Value(rtfRFC) [/quote]
I really like this idea!. Thank you :stuck_out_tongue:

Note that the dictionary method is best if there is a defined list of words you need to replace, which seems to be the case based on your example. Otherwise, Left(sourceWord)+“X” is better.

[quote=228558:@Marco Hof]An easy drop in solution:

If BadWords.HashKey(strRFC) Then DeleteProhibited = BadWords.Value(rtfRFC) [/quote]

This is functionally equivalent to:

DeleteProhibited=BadWords.Lookup(strRFC, DeleteProhibited)