How to remove \x00 from a string

  1. 7 months ago

    Beatrix W

    7 Feb 2019 Pre-Release Testers, Third Party Store Europe (Germany)

    Got an odd attachment name. The original data is

    =?ISO-8859-1?Q?AntragPromotionser=F6ffnung=5FKBo=5Fv2.doc=00?=

    which is quoted printable encoded ISO text, which is fine. At the end of the string, however, there is a \x00 character. Which makes code like

     dim theAttachment as FolderItem = GetFolderItem(name)
       NameExtension = getExtensionFromFile(Name)

    fail with an NOE. So I thought I'd simply get rid of the character. But how do I do this? A replace with "\x00" failed. Same for a more general regex:

    dim theRegex as new RegEx
    theRegex.options.ReplaceAllMatches = true
    theRegex.searchpattern = "\x00|\x01|\x02|\x03|\x04|\x05|\x06|\x07|\x08|\x0E|\x0F|\x10|\x11|\x12|\x13|\x14|\x15|\x16|\x17|\x18|\x19|\x1A|\x1B|\x1C|\x1D|\x1E"
    theRegex.replacementPattern = ""
    theFilename = theRegex.replace(theFilename)

    What am I missing here?

  2. Julian S

    7 Feb 2019 Pre-Release Testers, Xojo Pro UK
    Edited 7 months ago

    Whilst I cant comment on code you don't show, the code you do show works by removing the trailing 00 (when viewing the variables as binary in the debugger) using this code:

    Dim theFilename As String
    
    Dim s As String
    
    s = "=?ISO-8859-1?Q?AntragPromotionser=F6ffnung=5FKBo=5Fv2.doc=00?="
    
    Dim a As String
    
    a = DecodeQuotedPrintable(s)
    
    Dim theRegex As New RegEx
    theRegex.options.ReplaceAllMatches = True
    theRegex.searchpattern = "\x00|\x01|\x02|\x03|\x04|\x05|\x06|\x07|\x08|\x0E|\x0F|\x10|\x11|\x12|\x13|\x14|\x15|\x16|\x17|\x18|\x19|\x1A|\x1B|\x1C|\x1D|\x1E"
    theRegex.replacementPattern = ""
    
    theFilename = theRegex.replace(a)
    
    break

    You'd have to post how you get name out of the string to try and find the issue as it seems that name isn't correct if its throwing a NOE.

  3. Sascha S

    7 Feb 2019 Pre-Release Testers, Xojo Pro Germany, Lower Saxonary
    Edited 7 months ago

    @Beatrix W \x00|\x01|\x02|\x03|\x04|\x05|\x06|\x07|\x08|\x0E|\x0F|\x10|\x11|\x12|\x13|\x14|\x15|\x16|\x17|\x18|\x19|\x1A|\x1B|\x1C|\x1D|\x1E

    This won't help to solve your issue, but I think the above could be replaced with a \x0[xX][A-Fa-f0-9]+

  4. Beatrix W

    7 Feb 2019 Pre-Release Testers, Third Party Store Europe (Germany)

    The code is part of my upcoming Mime Parser. However, base64 always works for unprintable characters. The following does't remove the \x00:

    Dim theFilename As String
    
    Dim s As String = "QW50cmFnUHJvbW90aW9uc2Vyw7ZmZm51bmdfS0JvX3YyLmRvYwA="
    s = DecodeBase64(s)
    s = DefineEncoding(s, Encodings.UTF8)
    Dim theRegex As New RegEx
    theRegex.options.ReplaceAllMatches = True
    theRegex.searchpattern = "\x0[xX][A-Fa-f0-9]+"
    theRegex.replacementPattern = ""
    
    theFilename = theRegex.replace(s)

    In both s and theFilename I can see the \x00 at the end of the string.

  5. Julian S

    7 Feb 2019 Pre-Release Testers, Xojo Pro UK
    Dim theFilename As String
    
    Dim s As String = "QW50cmFnUHJvbW90aW9uc2Vyw7ZmZm51bmdfS0JvX3YyLmRvYwA="
    s = DecodeBase64(s)
    s = DefineEncoding(s, Encodings.UTF8)
    Dim theRegex As New RegEx
    theRegex.options.ReplaceAllMatches = True
    theRegex.searchpattern = "\x00|\x01|\x02|\x03|\x04|\x05|\x06|\x07|\x08|\x0E|\x0F|\x10|\x11|\x12|\x13|\x14|\x15|\x16|\x17|\x18|\x19|\x1A|\x1B|\x1C|\x1D|\x1E"
    theRegex.replacementPattern = ""
    
    theFilename = theRegex.replace(s)

    Works fine here, Windows 10, 2018r4

    Dim theFilename As String
    
    Dim s As String = "QW50cmFnUHJvbW90aW9uc2Vyw7ZmZm51bmdfS0JvX3YyLmRvYwA="
    s = DecodeBase64(s)
    s = DefineEncoding(s, Encodings.UTF8)
    Dim theRegex As New RegEx
    theRegex.options.ReplaceAllMatches = True
    theRegex.searchpattern = "\x0[xX][A-Fa-f0-9]+"
    theRegex.replacementPattern = ""
    
    theFilename = theRegex.replace(s)

    Doesn't work.

  6. Beatrix W

    7 Feb 2019 Pre-Release Testers, Third Party Store Europe (Germany)

    Thanks, Julian. Don't know what I did wrong in the morning.

  7. Kem T

    7 Feb 2019 Pre-Release Testers, Xojo Pro, XDC Speakers Connecticut

    FYI, you can specify ranges within a character class, like this:

    [\x00-\x08\x0E-\x1E]

    But I think I would have done this:

    [\x00-\x1F]

    If it's a filename, you wouldn't want any of these characters, including tab or an EOL character, right?

  8. Robert W

    7 Feb 2019 Western Canada

    Have you tried:
    ReplaceAll(inputString,chr(0),"")

or Sign Up to reply!