-
If i have the string "give me the #1"
how do i delete the # from the string?
given that the # could be anywhere in the string and there could be multiple #'s in the string?
Ex. "i am #1 and you are #2"
i want it to read "i am 1 and you are 2"
Thank You,
Mark S
-
try looking up instr and mid in VB help. You can find the # and delete it by replacing it with ""
-
Here's the easiest and quickest way:
Dim saParts() As String
Dim sWhole as String
sWhole = "i am #1 and you are #2"
saParts = Split(sWhole, "#")
sWhole = Join(saParts)
There you go! Split is VB's tokenizer method, which breaks apart a string into an array based on the character (string actually) that you specify. Join puts an array of strings together into a single string. Easy enough.
-
If you have VB6, then you can use Replace function.
Code:
Dim strText As String
strText = "i am #1 and you are #2"
strText = Replace(strText, "#", "")
If you have VB5 or earlier then you can use this function:
Code:
Public Function ReplaceText(pText As String, pFindString As String, pReplaceString As String) As String
Do Until InStr(pText, pFindString) = 0
pText = Left(pText, InStr(pText, pFindString) - 1) & _
Mid(pText, InStr(pText, pFindString) + Len(pFindString))
Loop
End Function
You can call it the same way:
Code:
Private Sub Command1_Click()
Dim strText As String
strText = "i am #1 and you are #2"
strText = ReplaceText(strText, "#", "")
MsgBox strText
End Sub
Regards,
-
This tip from Sam Hughill should get you on the right
path
Code:
Option Explicit
'----------------------------------------
'- Name: Sam Huggill
'- Email: [email protected]
'- Web: http://www.programmerz.com/vb/
'- Company: Lighthouse Internet Solutions
'----------------------------------------
'- Notes: Useful for databases
'
'----------------------------------------
'Applies to: VB3, 4 16/32, 5 and 6
'Level: Beginner
Public Function fConvert(ByVal sStr As String) As String
Dim i As Integer
Dim sBadChar As String
' List all illegal/unwanted characters
sBadChar = "#/<>?\|*:'"
' Loop through all the characters of the string
' checking whether each is an illegal character
For i = 1 To Len(sStr)
If InStr(sBadChar, Mid(sStr, i, 1)) Then
Mid(sStr, i, 1) = " "
End If
Next i
fConvert = sStr
'***********************************************
'******** Print the Conversion on the Form *****
Print sStr
'***********************************************
End Function
'put the following in your Forms Click Event
Private Sub Form_Click()
Dim MyString As String
MyString = "You are #1 and he is #2"
fConvert MyString
End Sub
-
Replace character
What if the character i want to replace is "
inputstring = "me"yes"
how do i get inputstring = "meyes"
rrrr= replace(inputstring, "''","") does not work.
Mark S.
-
In a string " is "" so your token is """".
Code:
Temp = Replace(inputstring, """","")
Well, I made also a getArgs function which ignores texts in "". If you need code just send me mail (because I don't look at this board very often ;)).
-
I personally don't like the """" in my code so if you don't either you could use:
Code:
rrrr = Replace(inputstring, Chr(34), vbNullString)
Just me being picky, sorry.
-
Hi, Mark.
VB won't allow you to do this: inputstring = "me"yes"
-
Sorry LG, but you can assign a value to a string that contains a quote.
For example:
inputstring = Text1.Text
(where Text1.Text is "me"yes")
This is because internally VB stores strings as character arrays where each character is in its ASCII code equivalent. This means that a string can contain anything you want it to, but if you try assign it directly then you can run into problems with the VB IDE.
If you try to assign it like...
inputstring = "me"yes"
...then yes, you will get an error.
But you can assign by specifying its character code and appending it to the string, ie:
inputstring = "me" & Chr(34) & "yes"
Sorry, but I just didn't want this to lead to anymore confusions.
-
Yes,SonGouki, you are absolutely right. I meant the way it was written before (inputstring = "me"yes").
Larisa