none
Replacing some special ASCII Charaters RRS feed

  • Question

  • Hi,

    I am currently writing a word add-in that will take a word document and write its parts into an xml document, I am have some problems with replaceing some rouge charaters: chr(7), the Bell charater; and chr(11), the Vertical Tab.

     

    this is the code I am using:

                            'Bell
                            rge = Chr(7)
                            text = Replace(text, rge, "")

                            'Vertical Tab
                            rge = Chr(11)
                            text = Replace(text, rge, "")

    this code works for ASCII charaters from 0-6, i beleve

    any help will be useful

    thankyou

    B

     

    • Moved by Jie BaoModerator Wednesday, April 13, 2011 6:20 AM (From:Visual Basic General)
    Monday, April 11, 2011 2:03 PM

Answers

  • I woudl advise against using the

    Microsoft.VisualBasic.Strings.Replace

    http://msdn.microsoft.com/en-us/library/microsoft.visualbasic.strings.replace.aspx

    And use the .NET

    System.String.replace method

    http://msdn.microsoft.com/en-us/library/system.string.replace.aspx

    The reasonin is this a framework method and therefore the same functionality is supported by all .NET languages (useful if you need to convert something to C#).

    Also the framework method is supported on all platforms, the Microsoft.VisualBasic namespace is not supported on some platforms (Windows Phone 7 as an example)

     

    Monday, April 11, 2011 3:46 PM
  • Hi again,

     

    Tip:

    Finally, try to avoid using text

    as a variable name as it can refer to the Text property of the Form

    or whatever you use text , Text or Me.Text within, such as the code for a custom control.

     

    Unless of course you want to write to ( set ) the Text property

    or retrieve ( get ) the string that is the Text property.

    :-)    ;-)    :-D

     



    Regards,

    John

    Click this link to see how to insert a picture into a forum post.

    Installing VB6 on Windows 7

    Monday, April 11, 2011 3:28 PM
  • ASCII is a binary standard for storing human-readable string values.  So while I totally agree with what spotty and JAO have to say (vis a vis using the DotNET methods instead of the Microsoft.VisualBasic methods), you will be doing yourself a big favor to visit a page that supplies the ASCII table (or to just google for ASCII table if you don't like the format you find at my link) and get familiar with the numeric value for each printable character. The values listed under the DEC column indicate what you're going to get out of binary (byte) values in VB. I actually have this particular ASCII table printed and pinned to the bulletin board over my desk - I tend to refer to it several times a day.

    This allows you to eliminate all the string manipulation and go straight to the numeric source, as demonstrated below:

     

    Public Class Form1
      Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'This is a byte array containing JUST the binary value for vertical tab - this is 
        'how your computer looks at or stores a string with just one character in it
        Dim Binary_ASCIIvTab As System.Byte() = New System.Byte() {11}
        'This is a byte array containing JUST the binary value for bell
        Dim Binary_ASCIIBell As System.Byte() = New System.Byte() {7}
        'This string contains some text as well as the ASCII-printable representations of the above-defined 
        'binary arrays
        Dim MyString As System.String = "This is a vertical tab: " & System.Text.Encoding.ASCII.GetString(Binary_ASCIIvTab) & System.Environment.NewLine & _
                        "This is a bell: " & System.Text.Encoding.ASCII.GetString(Binary_ASCIIBell) & System.Environment.NewLine
        'Look at how this prints
        MsgBox(MyString)
    
        'Now slam the string into a binary array
        Dim MyASCIIArray As System.Byte() = System.Text.Encoding.ASCII.GetBytes(MyString)
        'And set up a secondary array for output
        Dim MyOutputArray(-1) As System.Byte
        'And iterate through each value in the original data
        For Each char_byt As System.Byte In MyASCIIArray
          'Simply add additional OrElse bits to the following IF statement to add additional 
          'characters for processing, using the proper decimal values for the ASCII characters 
          'you want to process.
          If ((char_byt = 11) OrElse char_byt = (7)) Then
            'If this logic evaluates to TRUE, then we've encountered a vertical tab or a bell as 
            'represented by their raw binary values.
            '
            'Since it looks like your goal is to completely eliminate these characters, simply 
            'do nothing!
            '
            'Since this is a demo snippet, I'm going to replace the value with an ! character
            '
            'Expand the output array by its own length (remember the array length is 
            'always 1 value higher than its highest content-index)
            ReDim Preserve MyOutputArray(MyOutputArray.Length)
            'Now set the replacement value into the output array
            'Remember that its new length is 1 higher than its highest content-index, so
            'for the upper boundary position we use the length - 1.
            MyOutputArray(MyOutputArray.Length - 1) = 33
          Else
            'If the logic evaluates to FALSE, then we've encountered something else
            '
            'Expand the output array by its own length (remember the array length is 
            'always 1 value higher than its highest content-index)
            ReDim Preserve MyOutputArray(MyOutputArray.Length)
            '
            'Now set the discovered value into the output array
            'Remember that its new length is 1 higher than its highest content-index, so
            'for the upper boundary position we use the length - 1.
            MyOutputArray(MyOutputArray.Length - 1) = char_byt
          End If '((char_byt = 11) OrElse char_byt = (7))
        Next char_byt
    
        'Now slam it back to the original string.
        MyString = System.Text.Encoding.ASCII.GetString(MyOutputArray)
        'And look at what's printed now
        MsgBox(MyString)
      End Sub 'Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    End Class 'Form1
    


    It never hurts to try. In a worst case scenario, you'll learn from it.
    Monday, April 11, 2011 5:16 PM

All replies

  • Hi,

    The method you show relies on the

    Microsoft.VisualBasic

    namespace being added as a reference.

    Therefore the code you show is 'legacy' code from the days of VB6 and earlier.

     

    While it should work, the more modern VB.Net method is to follow the name of the string variable with

    .Replace

    as in the following code.

     

    Try this with one Button on a Form please:>>

     

     

     

    Public Class Form1
    
      Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
        Dim myText, rge As String
    
        myText = "Hi!" & New String(Chr(7), 20)
        'Bell
        rge = Chr(7)
    
        MessageBox.Show(myText & " string length = " & myText.Length.ToString, "Before...")
        myText = myText.Replace(rge, "")
        MessageBox.Show(myText & " string length = " & myText.Length.ToString, "After...")
    
        '---------------------------------------
    
        myText = "Hi!" & New String(Chr(11), 25)
        'Vertical Tab
        rge = Chr(11)
    
        MessageBox.Show(myText & " string length = " & myText.Length.ToString, "Before...")
        myText = myText.Replace(rge, "")
        MessageBox.Show(myText & " string length = " & myText.Length.ToString, "After...")
    
      End Sub
    End Class

     



    Regards,

    John

    Click this link to see how to insert a picture into a forum post.

    Installing VB6 on Windows 7


    Monday, April 11, 2011 3:02 PM
  • Hi again,

    If you wish to stick to

    using Replace in the way you show then you can if you wish.  :-)

    Here it is with a working example, again using one Button on a Form:>>

     

    Imports Microsoft.VisualBasic
    
    Public Class Form1
    
      Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
        Dim myText, rge As String
    
        myText = "Hi!" & New String(Chr(7), 20)
        'Bell
        rge = Chr(7)
    
        MessageBox.Show(myText & " string length = " & myText.Length.ToString, "Before...")
        myText = Replace(myText, rge, "")
        MessageBox.Show(myText & " string length = " & myText.Length.ToString, "After...")
    
        '---------------------------------------
    
        myText = "Hi!" & New String(Chr(11), 25)
        'Vertical Tab
        rge = Chr(11)
    
        MessageBox.Show(myText & " string length = " & myText.Length.ToString, "Before...")
        myText = Replace(myText, rge, "")
        MessageBox.Show(myText & " string length = " & myText.Length.ToString, "After...")
    
      End Sub
    End Class

     



    Regards,

    John

    Click this link to see how to insert a picture into a forum post.

    Installing VB6 on Windows 7

    Monday, April 11, 2011 3:11 PM
  • Hi again,

     

    Tip:

    Finally, try to avoid using text

    as a variable name as it can refer to the Text property of the Form

    or whatever you use text , Text or Me.Text within, such as the code for a custom control.

     

    Unless of course you want to write to ( set ) the Text property

    or retrieve ( get ) the string that is the Text property.

    :-)    ;-)    :-D

     



    Regards,

    John

    Click this link to see how to insert a picture into a forum post.

    Installing VB6 on Windows 7

    Monday, April 11, 2011 3:28 PM
  • I woudl advise against using the

    Microsoft.VisualBasic.Strings.Replace

    http://msdn.microsoft.com/en-us/library/microsoft.visualbasic.strings.replace.aspx

    And use the .NET

    System.String.replace method

    http://msdn.microsoft.com/en-us/library/system.string.replace.aspx

    The reasonin is this a framework method and therefore the same functionality is supported by all .NET languages (useful if you need to convert something to C#).

    Also the framework method is supported on all platforms, the Microsoft.VisualBasic namespace is not supported on some platforms (Windows Phone 7 as an example)

     

    Monday, April 11, 2011 3:46 PM
  • ASCII is a binary standard for storing human-readable string values.  So while I totally agree with what spotty and JAO have to say (vis a vis using the DotNET methods instead of the Microsoft.VisualBasic methods), you will be doing yourself a big favor to visit a page that supplies the ASCII table (or to just google for ASCII table if you don't like the format you find at my link) and get familiar with the numeric value for each printable character. The values listed under the DEC column indicate what you're going to get out of binary (byte) values in VB. I actually have this particular ASCII table printed and pinned to the bulletin board over my desk - I tend to refer to it several times a day.

    This allows you to eliminate all the string manipulation and go straight to the numeric source, as demonstrated below:

     

    Public Class Form1
      Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'This is a byte array containing JUST the binary value for vertical tab - this is 
        'how your computer looks at or stores a string with just one character in it
        Dim Binary_ASCIIvTab As System.Byte() = New System.Byte() {11}
        'This is a byte array containing JUST the binary value for bell
        Dim Binary_ASCIIBell As System.Byte() = New System.Byte() {7}
        'This string contains some text as well as the ASCII-printable representations of the above-defined 
        'binary arrays
        Dim MyString As System.String = "This is a vertical tab: " & System.Text.Encoding.ASCII.GetString(Binary_ASCIIvTab) & System.Environment.NewLine & _
                        "This is a bell: " & System.Text.Encoding.ASCII.GetString(Binary_ASCIIBell) & System.Environment.NewLine
        'Look at how this prints
        MsgBox(MyString)
    
        'Now slam the string into a binary array
        Dim MyASCIIArray As System.Byte() = System.Text.Encoding.ASCII.GetBytes(MyString)
        'And set up a secondary array for output
        Dim MyOutputArray(-1) As System.Byte
        'And iterate through each value in the original data
        For Each char_byt As System.Byte In MyASCIIArray
          'Simply add additional OrElse bits to the following IF statement to add additional 
          'characters for processing, using the proper decimal values for the ASCII characters 
          'you want to process.
          If ((char_byt = 11) OrElse char_byt = (7)) Then
            'If this logic evaluates to TRUE, then we've encountered a vertical tab or a bell as 
            'represented by their raw binary values.
            '
            'Since it looks like your goal is to completely eliminate these characters, simply 
            'do nothing!
            '
            'Since this is a demo snippet, I'm going to replace the value with an ! character
            '
            'Expand the output array by its own length (remember the array length is 
            'always 1 value higher than its highest content-index)
            ReDim Preserve MyOutputArray(MyOutputArray.Length)
            'Now set the replacement value into the output array
            'Remember that its new length is 1 higher than its highest content-index, so
            'for the upper boundary position we use the length - 1.
            MyOutputArray(MyOutputArray.Length - 1) = 33
          Else
            'If the logic evaluates to FALSE, then we've encountered something else
            '
            'Expand the output array by its own length (remember the array length is 
            'always 1 value higher than its highest content-index)
            ReDim Preserve MyOutputArray(MyOutputArray.Length)
            '
            'Now set the discovered value into the output array
            'Remember that its new length is 1 higher than its highest content-index, so
            'for the upper boundary position we use the length - 1.
            MyOutputArray(MyOutputArray.Length - 1) = char_byt
          End If '((char_byt = 11) OrElse char_byt = (7))
        Next char_byt
    
        'Now slam it back to the original string.
        MyString = System.Text.Encoding.ASCII.GetString(MyOutputArray)
        'And look at what's printed now
        MsgBox(MyString)
      End Sub 'Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    End Class 'Form1
    


    It never hurts to try. In a worst case scenario, you'll learn from it.
    Monday, April 11, 2011 5:16 PM
  • Hi Andrew,

    Just to let you know.  :-)

    MsgBox is also in the Microsoft.VisualBasic

    namespace.

    MessageBox.Show

    has more options and is the latest .Net method.

    It has a total of 21 method variations ( 1 + 20 overloads )

    when I've looked in VB.Net 2008.

     

    I need not nit-pick though as Chr and ChrW are also in the

    Microsoft.VisualBasic namespace and I've used Chr

    :-D

     



    Regards,

    John

    Click this link to see how to insert a picture into a forum post.

    Installing VB6 on Windows 7

    Monday, April 11, 2011 10:12 PM
  • Hi Andrew,

    Just to let you know.  :-)

    MsgBox is also in the Microsoft.VisualBasic


    It works fine for demo purposes.

    The only alternative is to post the entire code-behind for my custom application framework.  The MsgBox function defined there is actually a System.Windows.Forms.Form... I've posted the code for it repeatedly in other threads.


    It never hurts to try. In a worst case scenario, you'll learn from it.
    Monday, April 11, 2011 10:24 PM
  • The only alternative is to post the entire code-behind for my custom application framework.  The MsgBox function defined there is actually a System.Windows.Forms.Form... I've posted the code for it repeatedly in other threads.
    It never hurts to try. In a worst case scenario, you'll learn from it.

    Hi Andrew,

    Well that is certainly an idea I had not even considered,

    using your own MsgBox

    I mean and not using the Microsoft.VisualBasic namespace at all.

    However I have done my own MsgBox once, when I had a need

    for it only I called it something other than 'MsgBox' or 'MessageBox'.



    Regards,

    John

    Click this link to see how to insert a picture into a forum post.

    Installing VB6 on Windows 7

    Monday, April 11, 2011 10:44 PM
  • The only alternative is to post the entire code-behind for my custom application framework.  The MsgBox function defined there is actually a System.Windows.Forms.Form... I've posted the code for it repeatedly in other threads.
    It never hurts to try. In a worst case scenario, you'll learn from it.

    Hi Andrew,

    Well that is certainly an idea I had not even considered,

    using your own MsgBox

    I mean and not using the Microsoft.VisualBasic namespace at all.

    However I have done my own MsgBox once, when I had a need

    for it only I called it something other than 'MsgBox' or 'MessageBox'.




    My post is 4th in the thread.

    Pretty simple stuff. Sticking a custom function that just relays to the Messageboxes.MessageBox.MsgBox function at the public/shared level in an application makes a handy-dandy replacement when you turn off the application framework and/or unreference Microsoft.VisualBasic.  Especially handy when you aren't prepared to re-learn a new messagebox alias (or unlearn the old one).


    It never hurts to try. In a worst case scenario, you'll learn from it.
    Tuesday, April 12, 2011 12:39 AM
  • Hi Andrew,

    Thanks for the link to your code.    :-)

    That is the 1st time I've seen that fairly recent thread too.

     

    I'll try it later, I need to go to bed very soon. It is 2:08am here in the U.K.

    I'm up for work later this morning, near noon.



    Regards,

    John

    Click this link to see how to insert a picture into a forum post.

    Installing VB6 on Windows 7
    Tuesday, April 12, 2011 1:08 AM
  • That is the 1st time I've seen that fairly recent thread too.

    Yeah.  In my opinion they really did us all a huge disservice when they disabled the autoupdate on the front end.  Makes it really, really hard to keep up with stuff.

    They've posted some nonsense in the actual Forum Support forum about how it was adversely impacting forum performance... but I note there are still widgets on the frontpage that constantly auto-update, so pretty blatantly the forum performance that was being impacted had nothing to do with server resources or bandwidth.  Can't imagine what else would possess MSDN's dev squad to break the site like that...


    It never hurts to try. In a worst case scenario, you'll learn from it.
    Tuesday, April 12, 2011 1:42 AM
  • I woudl advise against using the

    Microsoft.VisualBasic.Strings.Replace

    http://msdn.microsoft.com/en-us/library/microsoft.visualbasic.strings.replace.aspx

    And use the .NET

    System.String.replace method

    http://msdn.microsoft.com/en-us/library/system.string.replace.aspx

    The reasonin is this a framework method and therefore the same functionality is supported by all .NET languages (useful if you need to convert something to C#).

    Also the framework method is supported on all platforms, the Microsoft.VisualBasic namespace is not supported on some platforms (Windows Phone 7 as an example)

     


    Hi everybody,

    thank you for replying, I have tested both ways in a windows form, they both work, but there seems to be a problem with the "bell" and "vertical tab" charaters, chr(7) and chr(11), in the word add-in

    Tuesday, April 12, 2011 2:22 PM
  • Does Word even support those characters?
    It never hurts to try. In a worst case scenario, you'll learn from it.
    Tuesday, April 12, 2011 2:36 PM
  • they are not visable, but they will be copied into the xml document, that is why I want to get rid of them as I copy them in
    Tuesday, April 12, 2011 2:39 PM
  • So what's the problem?

    It never hurts to try. In a worst case scenario, you'll learn from it.
    Tuesday, April 12, 2011 2:44 PM
  • I am sorry for not being clear in my original question, the two charaters in question are not being replaced, others, eg. chr(0) and others, are being replaced, but not those two
    Tuesday, April 12, 2011 3:12 PM
  • I posted code that will do the job and so did others.


    It never hurts to try. In a worst case scenario, you'll learn from it.
    Tuesday, April 12, 2011 3:56 PM