Results 1 to 11 of 11

Thread: Delete a specific charater

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Feb 2000
    Location
    Dallas,Tx,USA
    Posts
    30
    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




  2. #2
    Addicted Member
    Join Date
    Mar 2000
    Location
    Suffolk. UK
    Posts
    162
    try looking up instr and mid in VB help. You can find the # and delete it by replacing it with ""

  3. #3
    Addicted Member
    Join Date
    Aug 1999
    Location
    Ottawa,ON,Canada
    Posts
    217
    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.

  4. #4
    Serge's Avatar
    Join Date
    Feb 1999
    Location
    Scottsdale, Arizona, USA
    Posts
    2,744
    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,

  5. #5
    Addicted Member
    Join Date
    Jan 1999
    Posts
    239
    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

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Feb 2000
    Location
    Dallas,Tx,USA
    Posts
    30

    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.

  7. #7
    PowerPoster Fox's Avatar
    Join Date
    Jan 2000
    Location
    *afk*
    Posts
    2,088
    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 ).

  8. #8
    Addicted Member
    Join Date
    Aug 1999
    Location
    Ottawa,ON,Canada
    Posts
    217
    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.

  9. #9
    Hyperactive Member
    Join Date
    Jun 1999
    Posts
    308
    Hi, Mark.

    VB won't allow you to do this: inputstring = "me"yes"


  10. #10
    Addicted Member
    Join Date
    Aug 1999
    Location
    Ottawa,ON,Canada
    Posts
    217
    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.

  11. #11
    Hyperactive Member
    Join Date
    Jun 1999
    Posts
    308
    Yes,SonGouki, you are absolutely right. I meant the way it was written before (inputstring = "me"yes").

    Larisa


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width