Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Replace number with ascii character?

Status
Not open for further replies.

j420exe

MIS
Feb 17, 2002
28
US
I have a string: abc!246!def
I need to replace the 246 with the ö character which is 246 on the ASCII table.
Is there is a way to search the string for the ! and then replace whatever number is in between then ! with what ascii character it is?

Thanks!
 
myStr = "abc" + Chr(246) + "def"


myStr = "abc!246!def"
myStr = replace(myStr,"!246!,Chr(246))



-----------------------------------------------------------------
"The difference between 'involvement' and 'commitment' is like an eggs-and-ham breakfast: the chicken was 'involved' - the pig was 'committed'."
- unknown

mikewolf@tst-us.com
 
What if I don't know what the number is going to be between the !?

Thanks for the help!
 
myStr = "abc!246!def"

pos1 = cInt(inStr(myStr,"!"))
pos2 = cInt(inStr(pos1, myStr, "!"))
numVal = cInt(mid(inStr, pos1+1, pos2-pos1))

myStr = replace(myStr, "!" + cStr(numVal) + "!", Chr(numVal))
-----------------------------------------------------------------
"The difference between 'involvement' and 'commitment' is like an eggs-and-ham breakfast: the chicken was 'involved' - the pig was 'committed'."
- unknown

mikewolf@tst-us.com
 
Try something along the lines of...

Dim sBuf As String
Dim nStart As Integer
Dim nStop As Integer

sBuf = "abc!246!def"
nStart = InStr(sBuf, "!")
If nStart > 0 Then
nStop = InStr(nStart + 1, sBuf, "!")
If nStop > 0 Then
sBuf = Left(sBuf, nStart - 1) & _
Chr(CLng(Mid(sBuf, nStart + 1, (nStop - nStart) - 1))) & _
Mid(sBuf, nStop + 1)
End If
End If
 

Dim Results As String
Dim ary() As String
Dim i As Integer

ary = Split(TheString, "!")
For i = 0 To UBound(ary)
If i <> 1 Then Results = Results & ary(i)
Next i

Or,

Dim Pos As Integer
Pos = InStr(TheString, &quot;!&quot;)
If Pos Then TheString = Mid$(TheString, 1, Pos - 1) & Mid$(TheString, InStrRev(TheString, &quot;!&quot;) + 1)
[/b][/i][/u]*******************************************************
General remarks:
If this post contains any suggestions for the use or distribution of code, components or files of any sort, it is still your responsibility to assure that you have the proper license and distribution rights to do so!
 
Just another fun alternative:
[tt]
Public Function HackString(strSource As String) As String
Dim strTemp() As String

HackString = strSource
strTemp = Split(strSource, &quot;!&quot;)
If UBound(strTemp) > 1 Then
strTemp(1) = Chr(CLng(strTemp(1)))
HackString = Join(strTemp, &quot;&quot;)
End If

End Function
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top