Android Question Replace hex(00) with ""

Loris Anoardi

Member
Licensed User
Longtime User
Hello to everyone,
i need to replace hex(00) (null value) in a string by "" for removing it...
thanks for help
 

Beja

Expert
Licensed User
Longtime User
Hi Loris
if you could post some code then we may help..
 
Upvote 0

Loris Anoardi

Member
Licensed User
Longtime User
Sorry it was my mistake.
The string contains some points (.) at the end. It is a part of buffer received by the socket.
 
Upvote 0

MitchBu

Well-Known Member
Licensed User
Longtime User
Hello to everyone,
i need to replace hex(00) (null value) in a string by "" for removing it...
thanks for help

Use the string.replace function. Here is an example where "c" is replaced by "".
You do the same with your string and chr(0) instead of "c"

B4X:
    Dim strin As String = "abcdefghijklmnopq"
    Msgbox(strin.Replace("c",""),"Replaced")
 
Upvote 0

James Chamblin

Active Member
Licensed User
Longtime User
@MitchBu: seems as though Replace does not work with Chr(0). Here is a Sub that works.
B4X:
Sub Activity_Create(FirstTime As Boolean)
   'Do not forget to load the layout file created with the visual designer. For example:
   'Activity.LoadLayout("Layout1")
   Dim s As String = "abc" & Chr(0) & "def"
   Log(s & " " & s.length)
   s.Replace(Chr(0),"")
   Log(s & " " & s.Length)

   s = Remove0(s)
   Log(s & " " & s.Length)
End Sub

Sub Remove0 (s As String) As String
   Dim i As Int
   Dim NewString As String 'We will build the new string here
   Dim LeftIndex As Int = 0 'Keep track of what has already been copied over
   For i = 0 To s.Length - 1 'go through each character
     If s.CharAt(i) = Chr(0) Then
       NewString = NewString & s.SubString2(LeftIndex,i) 'copy the string up to the 0
       LeftIndex = i + 1 'increment the Left index just past the 0
     End If
   Next
   If LeftIndex < s.Length Then 'copy the rest of the string over
     NewString = NewString & s.SubString(LeftIndex)
   End If
   Return NewString 'return the result
End Sub
 
Upvote 0

MitchBu

Well-Known Member
Licensed User
Longtime User
@MitchBu: seems as though Replace does not work with Chr(0).

Thank you for spotting the Replace limit.

I had thought about doing a character by character scan until I found string.replace, but if it does not work, there is yet another way.

This sub is more generalized because it can replace strings of any length. I verified it does replace chr(0) :

B4X:
Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1")
    Dim strin As String = "abc" & Chr(0) & "defghijklmnopq"
    Msgbox(substitute(strin,Chr(0),"x"),"")
End Sub

Sub substitute(chain As String, search As String, replaced As String)
If chain.IndexOf(search) = 0 Then Return
Dim gauche, droite As String
Do While chain.IndexOf(search)>0
chain = chain.substring2(0,chain.IndexOf(search)) & replaced & _
chain.SubString(chain.IndexOf(search)+search.Length)
Loop
Return chain
End Sub
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…