Replace any chars not in allowed list

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Grok

    Replace any chars not in allowed list

    I need an elegant way to remove any characters in a string if they are
    not in an allowed char list. The part cleaning files of the
    non-allowed characters will run as a service, so no forms here.

    The list also needs to be editable by the end-user so I'll be
    providing a form on which they can edit the allowed character list.

    The end-user is non-technical so asking them to type a regular
    expression is out.

    Is there anything in the framework 2.0 to clean a string of unwanted
    characters, or better, clean any characters not in an allow list?
    Seems like there would be to clean SQL strings to prevent injection
    attacks.
  • schneider

    #2
    Re: Replace any chars not in allowed list


    string.Replace work fine for me:

    string Description = row["Desc"].ToString();
    Description = Description.Rep lace("\n", " | ");//Remove line feed
    Description = Description.Rep lace("\r", " | ");//Remove line feed
    char c=Convert.ToCha r(19);
    Description = Description.Rep lace(c, ":".ToCharArray ()[0]);


    schneider

    <Grokwrote in message news:o569r35aj5 0876urbr76bdi9h la7if7h0s@4ax.c om...
    >I need an elegant way to remove any characters in a string if they are
    not in an allowed char list. The part cleaning files of the
    non-allowed characters will run as a service, so no forms here.
    >
    The list also needs to be editable by the end-user so I'll be
    providing a form on which they can edit the allowed character list.
    >
    The end-user is non-technical so asking them to type a regular
    expression is out.
    >
    Is there anything in the framework 2.0 to clean a string of unwanted
    characters, or better, clean any characters not in an allow list?
    Seems like there would be to clean SQL strings to prevent injection
    attacks.

    Comment

    • Grok

      #3
      Re: Replace any chars not in allowed list

      Thanks but opposite of my needs. I am not looking for a way to
      replace a known character, but to remove any characters that are NOT
      in a given character list. Something like:

      Function CleanAString(By Val theDirtyString As String) As String
      Dim allowedChars As String
      allowedChars = GetAllowedChars FromRegKey()
      Return String.AwesomeS tringCleaner(th eDirtyString, allowedChars)
      End Function

      with "abcdef" on allowed list,
      CleanAString("a a+bb,cc112233/dd\ee:ff")
      returns "aabbccddee ff"


      On Thu, 14 Feb 2008 14:02:19 -0600, "schneider"
      <eschneider.new s.ms@starkinves tments.comwrote :
      >
      >string.Repla ce work fine for me:
      >
      >string Description = row["Desc"].ToString();
      >Description = Description.Rep lace("\n", " | ");//Remove line feed
      >Description = Description.Rep lace("\r", " | ");//Remove line feed
      >char c=Convert.ToCha r(19);
      >Description = Description.Rep lace(c, ":".ToCharArray ()[0]);
      >
      >
      >schneider
      >
      ><Grokwrote in message news:o569r35aj5 0876urbr76bdi9h la7if7h0s@4ax.c om...
      >>I need an elegant way to remove any characters in a string if they are
      >not in an allowed char list. The part cleaning files of the
      >non-allowed characters will run as a service, so no forms here.
      >>
      >The list also needs to be editable by the end-user so I'll be
      >providing a form on which they can edit the allowed character list.
      >>
      >The end-user is non-technical so asking them to type a regular
      >expression is out.
      >>
      >Is there anything in the framework 2.0 to clean a string of unwanted
      >characters, or better, clean any characters not in an allow list?
      >Seems like there would be to clean SQL strings to prevent injection
      >attacks.
      >

      Comment

      • Alex Clark

        #4
        Re: Replace any chars not in allowed list

        Dim tempString As String = theDirtyString

        For Each c as Char In theDirtyString
        If Not allowedChars.Co ntains(c.ToStri ng) Then
        tempString = tempString.Repl ace(c.ToString, String.Empty)
        End If
        Next

        Return tempString

        Something like that...?



        "Grok" <grok@valhallal egends.comwrote in message
        news:ks89r3ljfs mpn9149u480edt1 mqqsdjnel@4ax.c om...
        Thanks but opposite of my needs. I am not looking for a way to
        replace a known character, but to remove any characters that are NOT
        in a given character list. Something like:
        >
        Function CleanAString(By Val theDirtyString As String) As String
        Dim allowedChars As String
        allowedChars = GetAllowedChars FromRegKey()
        Return String.AwesomeS tringCleaner(th eDirtyString, allowedChars)
        End Function
        >
        with "abcdef" on allowed list,
        CleanAString("a a+bb,cc112233/dd\ee:ff")
        returns "aabbccddee ff"
        >
        >
        On Thu, 14 Feb 2008 14:02:19 -0600, "schneider"
        <eschneider.new s.ms@starkinves tments.comwrote :
        >
        >>
        >>string.Replac e work fine for me:
        >>
        >>string Description = row["Desc"].ToString();
        >>Description = Description.Rep lace("\n", " | ");//Remove line feed
        >>Description = Description.Rep lace("\r", " | ");//Remove line feed
        >>char c=Convert.ToCha r(19);
        >>Description = Description.Rep lace(c, ":".ToCharArray ()[0]);
        >>
        >>
        >>schneider
        >>
        >><Grokwrote in message news:o569r35aj5 0876urbr76bdi9h la7if7h0s@4ax.c om...
        >>>I need an elegant way to remove any characters in a string if they are
        >>not in an allowed char list. The part cleaning files of the
        >>non-allowed characters will run as a service, so no forms here.
        >>>
        >>The list also needs to be editable by the end-user so I'll be
        >>providing a form on which they can edit the allowed character list.
        >>>
        >>The end-user is non-technical so asking them to type a regular
        >>expression is out.
        >>>
        >>Is there anything in the framework 2.0 to clean a string of unwanted
        >>characters, or better, clean any characters not in an allow list?
        >>Seems like there would be to clean SQL strings to prevent injection
        >>attacks.
        >>

        Comment

        • Grok

          #5
          Re: Replace any chars not in allowed list

          Clever and perfect!

          On Thu, 14 Feb 2008 19:10:09 -0600, "Alex Clark" <alex@microsoft .com>
          wrote:
          >Dim tempString As String = theDirtyString
          >
          >For Each c as Char In theDirtyString
          If Not allowedChars.Co ntains(c.ToStri ng) Then
          tempString = tempString.Repl ace(c.ToString, String.Empty)
          End If
          >Next
          >
          >Return tempString
          >
          >Something like that...?
          >
          >
          >
          >"Grok" <grok@valhallal egends.comwrote in message
          >news:ks89r3ljf smpn9149u480edt 1mqqsdjnel@4ax. com...
          >Thanks but opposite of my needs. I am not looking for a way to
          >replace a known character, but to remove any characters that are NOT
          >in a given character list. Something like:
          >>
          >Function CleanAString(By Val theDirtyString As String) As String
          > Dim allowedChars As String
          > allowedChars = GetAllowedChars FromRegKey()
          > Return String.AwesomeS tringCleaner(th eDirtyString, allowedChars)
          >End Function
          >>
          >with "abcdef" on allowed list,
          >CleanAString(" aa+bb,cc112233/dd\ee:ff")
          >returns "aabbccddee ff"
          >>
          >>
          >On Thu, 14 Feb 2008 14:02:19 -0600, "schneider"
          ><eschneider.ne ws.ms@starkinve stments.comwrot e:
          >>
          >>>
          >>>string.Repla ce work fine for me:
          >>>
          >>>string Description = row["Desc"].ToString();
          >>>Descriptio n = Description.Rep lace("\n", " | ");//Remove line feed
          >>>Descriptio n = Description.Rep lace("\r", " | ");//Remove line feed
          >>>char c=Convert.ToCha r(19);
          >>>Descriptio n = Description.Rep lace(c, ":".ToCharArray ()[0]);
          >>>
          >>>
          >>>schneider
          >>>
          >>><Grokwrote in message news:o569r35aj5 0876urbr76bdi9h la7if7h0s@4ax.c om...
          >>>>I need an elegant way to remove any characters in a string if they are
          >>>not in an allowed char list. The part cleaning files of the
          >>>non-allowed characters will run as a service, so no forms here.
          >>>>
          >>>The list also needs to be editable by the end-user so I'll be
          >>>providing a form on which they can edit the allowed character list.
          >>>>
          >>>The end-user is non-technical so asking them to type a regular
          >>>expression is out.
          >>>>
          >>>Is there anything in the framework 2.0 to clean a string of unwanted
          >>>characters , or better, clean any characters not in an allow list?
          >>>Seems like there would be to clean SQL strings to prevent injection
          >>>attacks.
          >>>
          >

          Comment

          • Andrew Morton

            #6
            Re: Replace any chars not in allowed list

            Grok wrote:
            Is there anything in the framework 2.0 to clean a string of unwanted
            characters, or better, clean any characters not in an allow list?
            Seems like there would be to clean SQL strings to prevent injection
            attacks.
            If SQL is your objective then consider using parameterised queries instead:
            they are automatically injection-proof.

            Andrew


            Comment

            • Steve Gerrard

              #7
              Re: Replace any chars not in allowed list

              Grok wrote:
              Clever and perfect!
              >
              On Thu, 14 Feb 2008 19:10:09 -0600, "Alex Clark" <alex@microsoft .com>
              wrote:
              >
              >Dim tempString As String = theDirtyString
              >>
              >For Each c as Char In theDirtyString
              > If Not allowedChars.Co ntains(c.ToStri ng) Then
              > tempString = tempString.Repl ace(c.ToString, String.Empty)
              > End If
              >Next
              >>
              >Return tempString
              >>
              A little more efficent, maybe?

              Dim SB As New System.Text.Str ingBuilder

              For Each c As Char In theDirtyString
              If allowedChars.In dexOf(c) 0 Then
              SB.Append(c)
              End If
              Next c

              Return SB.ToString



              Comment

              • Alex Clark

                #8
                Re: Replace any chars not in allowed list

                A little more efficent, maybe?
                >
                Dim SB As New System.Text.Str ingBuilder
                >
                For Each c As Char In theDirtyString
                If allowedChars.In dexOf(c) 0 Then
                SB.Append(c)
                End If
                Next c
                >
                Return SB.ToString
                Memory-wise yes, StringBuilder will be far easier on the working set (and
                CPU) than just using System.String (and optimisation was left up to the OP
                as far as I was concerned). However, your solution is technically
                incorrect. IndexOf is 0 based in .NET, therefore you should have a <0
                rather than 0.

                Happy to help ;-P



                Comment

                Working...