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
 
null cannot be inside a string. If a string "contains" null then you have an string which contains the text "null".
the hole string can be null but not a part of it.

Code: Copy
if stringname = null then
  stringname=""
end if
 
Upvote 0
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" :)

Code: Copy
    Dim strin As String = "abcdefghijklmnopq"
    Msgbox(strin.Replace("c",""),"Replaced")
 
Upvote 0
@MitchBu: seems as though Replace does not work with Chr(0). Here is a Sub that works.
Code: Copy
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: 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) :

Code: Copy
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
Back
Top