Results 1 to 3 of 3

Thread: remove special characters from string

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2000
    Location
    Minnesota
    Posts
    830

    remove special characters from string

    Is there a build in function that removes the following without doing a replace for each character?

    "NUL", "SOH", "STX", "ETX", "EOT", "ENQ", "ACK", "BEL",
    "BS", "HT", "LF", "VT", "FF", "CR", "SO", "SI",
    "DLE", "DC1", "DC2", "DC3", "DC4", "NAK", "SYN", "ETB",
    "CAN", "EM", "SUB", "ESC", "FS", "GS", "RS", "US"

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: remove special characters from string

    You've got no choice but to do each one, but there are various ways to do it. This would be the most succinct option:
    vb.net Code:
    1. Dim builder As New StringBuilder("NULaSTXbSOHcSTX")
    2. Dim substrings = {"NUL", "SOH", "STX"}
    3.  
    4. Array.ForEach(substrings, Sub(s) builder.Replace(s, String.Empty))
    5.  
    6. MessageBox.Show(builder.ToString())
    Using a StringBuilder makes it much more efficient than a String for many replacements.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    New Member
    Join Date
    May 2011
    Location
    New York
    Posts
    12

    Re: remove special characters from string

    Use regular expressions.

    Code:
    Imports System.Text.RegularExpressions
    
    TargetString = Regex.Replace(TargetString, "NUL|SOH|STX|ETX", "")
    Just enter all the substrings as I showed with |'s separating them. Each of them will be replaced with nothing.

    This will help you build additional regular expressions: http://www.regular-expressions.info/reference.html

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width