How To Implement Replace Function Without Using The Inbuilt String Function

2
21
• 1
•
•
•
•
•
•
•
•
•
1
Share

A question was asked to me by one of the reputed firms that can you replace a few characters by another set of characters without using the inbuilt string function…..

Oh… this was a very popular topic during the last few days …finally I found the solution…
I am sharing this function below written…

It is changing HYD with CYB…

``````
Sub Macro1()

Dim bText() As Byte
Dim j As Integer
j = 0
Dim k As Integer
k = 0

Dim finalq As String
finalq = ""
'Assign the sting with null values
tttt = Len(inputq) + 3
ReDim a(tttt)
Dim MyString As String: MyString = inputq
'Check the string one by one..character by character
bText = MyString
For i = 0 To UBound(bText) - 1
If (bText(i) = 0) Then
Else
a(j) = Chr(bText(i))
j = j + 1
End If
Next
Do
If a(k) = "" Then
Else
'Checking Upper case and Lower case
If ((a(k) = "H" Or a(k) = "h") And (a(k + 1) = "Y" Or a(k + 1) = "y") And (a(k + 2) = "D" Or a(k + 2) = "d")) Then
If a(k) = "H" Then
a(k) = "C"
Else
a(k) = "c"
End If
If (a(k + 1) = "Y") Then
a(k + 1) = "Y"
Else
a(k + 1) = "y"
End If
If (a(k + 2) = "D") Then
a(k + 2) = "B"
Else
a(k + 2) = "b"
End If
finalq = finalq & a(k) & a(k + 1) & a(k + 2)
k = k + 2
Else
finalq = finalq & a(k)
End If
End If
k = k + 1
Loop Until (k = UBound(a))
MsgBox finalq
inputx = InputBox("Want to do again")
If inputx = "Y" Then
Call Macro1
Else
End If
End Sub
``````

We are done. This can be more dynamic by using replacing character is taken from the user.

Don't miss out!

Receive top technical news, lesson ideas, travel tips and more!

Give it a try. You can unsubscribe at any time.

• 1
•
•
•
•
•
•
•
•
•
1
Share

1. Anonymous