Non-Blank Space Extension Method

After struggling with this I’ve manage to get my emails (HTML Body) etc. working much better.

Public Function ReplaceNBSP(Extends value As String, html As Boolean = False) As String
  Static NBSP As String
  
  If NBSP = "" Then
    NBSP = ChrB(&hE2) + ChrB(&H80) + ChrB(&HAF) 
  End If
  
  If html Then
    Return value.ReplaceAll(NBSP, " ")
  Else
    Return value.ReplaceAll(NBSP, " ")
  End If
  
End Function

3 Likes

That will only work for strings that were UTF-8 but have been mis-identified as having an 8-bit encoding (MacRoman, Windows Latin-1, etc). A UTF-8 string could be unexpectedly altered by your code and would definitely not have its nonbreaking space characters replaced.

I’ve attached a module that does what you’re looking for in an admittedly more baroque fashion. It does require you to know the status of your NBSP situation – you have to know whether the string is really UTF-8 or mis-encoded, because it affects how you perform the ReplaceAll.

StringExtensionsForNBSP.xojo_binary_code.zip (1.5 KB)

Thanks for your input @Eric_Williams. I’ve only ever had this issue in the space between the time & am/pm from DateTime.ToString.

On my Mac Sonoma system, DateTime.ToString returns a properly encoded UTF-8 string, so your function should fail on it - but curiously, it does not.

This appears to be a bug. It looks like ReplaceAll does a binary replace when the “find” string has a Nil encoding.

Yeah, I do think this is a bug. Filed an issue:

https://tracker.xojo.com/xojoinc/xojo/-/issues/77262