Click to See Complete Forum and Search --> : Removing a character from a string


Ehsan
April 11th, 2001, 05:23 AM
Hi

How can I remove a character from a string?

Thanks
Ehsan

TH1
April 11th, 2001, 05:31 AM
If you want to remove all occurences of a character use the Replace function
Mystring="Hello"
MyString=replace(Mystring,"o","")

Cimperiali
April 11th, 2001, 05:48 AM
...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.

shree
April 11th, 2001, 08:07 AM
Using CopyMemory() is much faster than using Left$ and Right$ to rebuild another string.

Cimperiali
April 11th, 2001, 10:03 AM
All right, now I know. Thanks for sharing.
Cesare Imperiali

Special thanks to Lothar "the Great" Haensler. Come back soon, you Guru.

Cimperiali
April 11th, 2001, 10:26 AM
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.

shree
April 11th, 2001, 11:18 AM
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

shree
April 11th, 2001, 11:20 AM
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

Cimperiali
April 12th, 2001, 09:22 AM
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.