Android Question How to trap a non breaking space - solved!

Mark Read

Well-Known Member
Licensed User
Longtime User
Can anyone help me here?

I am reading a HTML file, line by line using Textreader. I then read the line one character at a time and process various characters. The HTML file shows the nbsp (cannot type the full code here) in the text and I need to find the code to use with the character command to remove this. I have tried 255 and 160 but they do not work.

B4X:
Select line.SubString2(c,c+1)
Case Chr(160)
     NewLine=NewLine & ""  'strip &nbsp

Best regards

@Erel,

thanks for the email help. I did not realise that we cannot use a B4A command word in the normal text!
 

Mark Read

Well-Known Member
Licensed User
Longtime User
Sorry people but I need to bump this thread. This problem is killing me.

This is an example of the html code
B4X:
            <b><a href="/" title="Search Torrents">Search Torrents</a></b>&nbsp;&nbsp;|&nbsp;

I need to remove the last three non breaking spaces after the end of bold tag.

Tried:
character 160 - not found
character 255 - not found

checking for "&" as the first character in a series causes a crash with boolean error!

Is this only one character (I know it shows in the browser as a space) or is it really 5 characters?

Has anyone an idea?
Thanks
Mark
 
Upvote 0

NJDude

Expert
Licensed User
Longtime User
Use REPLACE, something like this:
B4X:
Dim myVar As String
 
myVar = "<b><a href='/' title='Search Torrents'>Search Torrents</a></b>&nbsp;&nbsp;|&nbsp;"
myVar = myVar.Replace("&nbsp;", "")
 
Msgbox(myVar, "")
 
Upvote 0

Mark Read

Well-Known Member
Licensed User
Longtime User
NJDude, thanks for the reply but I did say that I am reading the line character by character and using select case to trap certain characters. In this case I would need to trap all five characters one by one. I had this idea as well (read first character, set a flag and trap the other 5) but select case crashes when I try to catch "&", not sure why (boolean error???). This is why I need the ASCII character number. It should be 160 I think but that does not work.

Any other ideas?
Thx
Mark
 
Upvote 0

NJDude

Expert
Licensed User
Longtime User
A more efficient way would be using the method I illustrated above, that way you can replace characters more easily instead of one by one, I don't know how your code looks like but you might need to reconsider your method. Just a suggestion.
 
Upvote 0

Mark Read

Well-Known Member
Licensed User
Longtime User
Thx NJDude, but....

To show what I mean, I have this short code:
B4X:
Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
    Dim AString, BString As String
    Dim c As Int
    Dim flag As Boolean
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1")
    AString="abcd&nbsp;efghnbsp"
    Log("A=" & AString)
    For c=0 To AString.Length-1
        Select AString.SubString2(c,c+1)
        Case "&" OR "n" OR "b" OR "s" OR "p" OR ";"
            If AString.SubString2(c,c+1)="&" Then
                flag=True
            End If
           
            If flag=True Then
                BString=BString+""
            Else
                BString=BString+AString.SubString2(c,c+1)
            End If
           
            If AString.SubString2(c,c+1)=";" AND flag=True Then
                flag=False
            End If
           
        Case Else
            BString=BString+AString.SubString2(c,c+1)
        End Select
    Next
   
    Log("B=" & BString)   
       
           
End Sub

This code does not run as I would expect it to, it gives this error:

** Activity (main) Create, isFirst = true **

A=abcd&nbsp;efghnbsp

main_activity_create (B4A line: 35)

Select AString.SubString2(c,c+1)
java.lang.RuntimeException: Cannot parse: & as boolean


at anywheresoftware.b4a.BA.parseBoolean(BA.java:515)
at anywheresoftware.b4a.BA.ObjectToBoolean(BA.java:582)
at mr.test.app.main._activity_create(main.java:258)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:169)
at mr.test.app.main.afterFirstLayout(main.java:89)
at mr.test.app.main.access$100(main.java:16)
at mr.test.app.main$WaitForLayout.run(main.java:74)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4627)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
at dalvik.system.NativeStart.main(Native Method)
java.lang.RuntimeException: Cannot parse: & as boolean

Does this help to clarify my problem? If my code finds a n b s or p, it stores it. Only if the & is first and the ; at the end, should all six characters be deleted.

Hopeing for a solution.
Mark
 
Upvote 0

NJDude

Expert
Licensed User
Longtime User
You just want to get rid of the "&nbsp" or you are doing other processing?, also, you have an error, you should replace this line:

From:
B4X:
BString = BString + ""
To:
B4X:
BString = BString & ""
Not sure why you are doing that but anyway, it is incorrect.
 
Last edited:
Upvote 0

Mark Read

Well-Known Member
Licensed User
Longtime User
@NJDude, yes - lose "&nbsp;" and yes - further processing. You are also quite right. Strings are joined with "&" not "+" - silly mistake (and that 3 times in the code).
BUT you have given me the solution! I am reading a line of code with textreader

B4X:
line=TextReader1.ReadLine

and the solution was simple, this line directly after:

B4X:
If line.Contains("&nbsp;") Then line=line.Replace("&nbsp;","")

Then I can continue with my select case etc. And it works!

Sometimes the answer is there all the time. Thanks very much.

PS. The code above now loooks like this but still crashes with theerror above. It is not part of the thread solution but do you know why??

B4X:
Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
    Dim AString, BString As String
    Dim c As Int
    Dim flag As Boolean
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1")
    AString="abcd&nbsp;efghnbsp"
    Log("A=" & AString)
    For c=0 To AString.Length-1
        Select AString.SubString2(c,c+1)
        Case "&" OR "n" OR "b" OR "s" OR "p" OR ";"
            If AString.SubString2(c,c+1)="&" Then
                flag=True
            End If
           
            If flag=True Then
                BString=BString & ""
            Else
                BString=BString & AString.SubString2(c,c+1)
            End If
           
            If AString.SubString2(c,c+1)=";" AND flag=True Then
                flag=False
            End If
           
        Case Else
            BString=BString & AString.SubString2(c,c+1)
        End Select
    Next
   
    Log("B=" & BString)   
       
           
End Sub

Thanks
Mark
 
Upvote 0

Mark Read

Well-Known Member
Licensed User
Longtime User
OMG! Maybe I should go back to reading the beginners guide!!! LOL

Many thanks NJDude
Mark
 
Upvote 0
Top