Stripping out unwanted characters

Discussion in 'ASP .Net' started by et, Feb 22, 2006.

  1. et

    et Guest

    How can I strip out unwanted characters in a string before updating the
    database? For instance, in names & addresses in our client table, we want
    only letters and numbers, no punctuation. Is there a way to do this?
    et, Feb 22, 2006
    #1
    1. Advertising

  2. et

    vinu Guest

    HI,

    Use regular expression to remove unwanted characters and then send the
    string to database. Here is a sample code to remove non-alphanumerical
    characters from a string.

    hoep this will help you...

    public static void Main()

    {


    /*Reg expression to find non-alphanumeric characters*/

    string pattern = @"[^A-Za-z0-9]";

    /*include System.Text.RegularExpressions name space*/

    Regex rgx = new Regex(pattern);

    string inputStr = "ab$!Cd&%$gf£!";

    /*Replace non-alphanumeric characters with space*/

    string outputStr = rgx.Replace(inputStr, " ");


    Console.WriteLine("Output string:"+ outputStr);

    }

    Cheers
    Vinu


    "et" <> wrote in message
    news:...
    > How can I strip out unwanted characters in a string before updating the
    > database? For instance, in names & addresses in our client table, we want
    > only letters and numbers, no punctuation. Is there a way to do this?
    >
    vinu, Feb 22, 2006
    #2
    1. Advertising

  3. Function StripUnwantedChars(ByVal StringToStrip AsString) AsString
    Dim stripped AsString
    If StringToStrip <> "" Then
    stripped = Regex.Replace(StringToStrip, "<(.|\n)+?>", String.Empty)
    Return stripped
    Else
    Return ""
    EndIf
    EndFunction

    Note : insert all the characters you want to strip inside the quotes.

    For example, instead of :
    stripped = Regex.Replace(StringToStrip, "<(.|\n)+?>", String.Empty)

    use :
    stripped = Regex.Replace(StringToStrip, "<!#(.|\n)+?>", String.Empty)





    Juan T. Llibre, asp.net MVP
    aspnetfaq.com : http://www.aspnetfaq.com/
    asp.net faq : http://asp.net.do/faq/
    foros de asp.net, en español : http://asp.net.do/foros/
    ===================================
    "et" <> wrote in message news:...
    > How can I strip out unwanted characters in a string before updating the database? For instance,
    > in names & addresses in our client table, we want only letters and numbers, no punctuation. Is
    > there a way to do this?
    >
    Juan T. Llibre, Feb 22, 2006
    #3
  4. et

    et Guest

    Thanks, everyone, that's exactly what I was looking for.


    "et" <> wrote in message
    news:...
    > How can I strip out unwanted characters in a string before updating the
    > database? For instance, in names & addresses in our client table, we want
    > only letters and numbers, no punctuation. Is there a way to do this?
    >
    et, Feb 22, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?THU=?=
    Replies:
    4
    Views:
    1,448
    Joerg Jooss
    Sep 2, 2005
  2. VJ

    Stripping characters...

    VJ, Jan 17, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    443
    John Timney \( MVP \)
    Jan 17, 2006
  3. Replies:
    1
    Views:
    435
    Hans Kesting
    Jun 1, 2006
  4. Edward Elliott

    stripping unwanted chars from string

    Edward Elliott, May 4, 2006, in forum: Python
    Replies:
    7
    Views:
    410
    Alex Martelli
    May 4, 2006
  5. Wild Al

    Stripping unwanted html

    Wild Al, Oct 6, 2006, in forum: Ruby
    Replies:
    5
    Views:
    104
    eden li
    Oct 9, 2006
Loading...

Share This Page