CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Mar 2001
    Posts
    26

    Removing a character from a string

    Hi

    How can I remove a character from a string?

    Thanks
    Ehsan


  2. #2
    Join Date
    Feb 2000
    Location
    Ireland
    Posts
    808

    Re: Removing a character from a string

    If you want to remove all occurences of a character use the Replace function
    Mystring="Hello"
    MyString=replace(Mystring,"o","")


  3. #3
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    Re: Removing a character from a string

    ...and you can use replace funtion to remove only one occurence of char you want. Look at vb Help.
    If you prefer to "manually" do this job, you can use instr function to find where characters you're looking for are and mid, left, and right to rebuild your string in another variable to substitute the first string...


    Special thanks to Lothar "the Great" Haensler. Come back soon, you Guru.
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

  4. #4
    Join Date
    Mar 1999
    Location
    Nepal
    Posts
    540

    Re: Removing a character from a string

    Using CopyMemory() is much faster than using Left$ and Right$ to rebuild another string.


  5. #5
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    Re: Removing a character from a string

    All right, now I know. Thanks for sharing.
    Cesare Imperiali

    Special thanks to Lothar "the Great" Haensler. Come back soon, you Guru.
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

  6. #6
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    Re: Removing a character from a string

    It seems I have some matters using this api. Have you any suggestion?

    Option Explicit
    Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)

    Private Sub Command1_Click()
    Dim x As String, y As String
    y = Text1.Text
    'CopyMemory x, y, Len(y) 'this copy the entire word (text1)
    CopyMemory x, y, 1 'this crashes my machine
    Label1.Caption = x
    End Sub

    Special thanks to Lothar "the Great" Haensler. Come back soon, you Guru.
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

  7. #7
    Join Date
    Mar 1999
    Location
    Nepal
    Posts
    540

    Re: Removing a character from a string

    Interpreting a string as a byte array helps. The following code copies any length of the string, just specify 2*(length of substring) as the length parameter.


    option Explicit
    private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination as Any, Source as Any, byval Length as Long)

    private Sub Command1_Click()
    Dim x(5) as Byte, y() as Byte
    y = "12345678901234567890"
    CopyMemory x(0), y(0), 8
    Label1.Caption = x
    End Sub





  8. #8
    Join Date
    Mar 1999
    Location
    Nepal
    Posts
    540

    Re: Removing a character from a string

    Interpreting a string as a byte array helps. The following code copies any length of the string, just specify 2*(length of substring) as the length parameter.


    option Explicit
    private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination as Any, Source as Any, byval Length as Long)

    private Sub Command1_Click()
    Dim x(10) as Byte, y() as Byte
    y = "12345678901234567890"
    CopyMemory x(0), y(0), 8
    Label1.Caption = x
    End Sub





  9. #9
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    Re: Removing a character from a string

    Thanks a lot. Tomorrow I will rate you (out of vote for today!)
    Cesare Imperiali

    Special thanks to Lothar "the Great" Haensler. Come back soon, you Guru.
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

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

Featured