Strip chars from a string

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

    #1

    Strip chars from a string

    Tom, Nikolay:
    That code doesn't work, at least not in VS2005. What happens is that
    when you replace with VBNullChar, it basically chops off the string
    from that point onwards. So Sna?*|fu" would become Sna instead of
    Snafu. If you use two double quotes, then it works as you expect.
    Here is corrected code:
    strNewFileName = InputBox("Enter the new base file name: ")
    ' Check for invalid filename characters.
    For Each invalidChar As Char In
    System.IO.Path. GetInvalidFileN ameChars
    strNewFileName = strNewFileName .Replace(invali dChar,
    "")
    Next

    Note that there is also a System.IO.Path. GetInvalidPathC hars function.
    Not really sure what the difference is but I guess there must be some
    characters that are allowed in the filename but not the folder name, or
    vice-versa. Also, System.IO.Path. InvalidPathChar s has been replaced
    by the two functions I mentioned above - you get an error if you try to
    use System.IO.Path. InvalidPathChar s in VS2005.



    =============== =============== =============== =============== =======
    From: Tom Shelton
    Date: Thurs, Feb 10 2005 2:39 am
    Email: Tom Shelton <tshel...@YOUKN OWTHEDRILLcomca st.net>
    Groups: microsoft.publi c.dotnet.langua ges.vb

    On 2005-02-09, Nikolay Petrov <johntup2_nosp. ..@mail.bg> wrote:
    [color=blue]
    > I need a way to strip chars from a string. The chars are all chars that are
    > not allowed in file path.[/color]
    [color=blue]
    > TIA[/color]


    What ever way you do it - I would make sure that you get the list of
    illegal chars from the System.IO.Path class's InvalidPathChar s
    property.

    One way, would be to build a regular expression (as Herfried
    suggested).
    Or you could do it something like:


    For Each invalidChar As Char In System.IO.Path. InvalidPathChar s
    thePathString = thePathString.R eplace (invalidChar, vbNullChar)

    Next


    HTH
    --
    Tom Shelton [MVP]

  • vbnetdev

    #2
    Re: Strip chars from a string

    Function CleanInput(ByVa l strIn As String) As String
    Return Regex.Replace(s trIn, "[^\w\.@-]", "")
    End Function



    --
    Get a powerful web, database, application, and email hosting with KJM
    Solutions




    "ImageAnaly st" <hayworth@hotma il.com> wrote in message
    news:1146684271 .774048.256410@ v46g2000cwv.goo glegroups.com.. .[color=blue]
    > Tom, Nikolay:
    > That code doesn't work, at least not in VS2005. What happens is that
    > when you replace with VBNullChar, it basically chops off the string
    > from that point onwards. So Sna?*|fu" would become Sna instead of
    > Snafu. If you use two double quotes, then it works as you expect.
    > Here is corrected code:
    > strNewFileName = InputBox("Enter the new base file name: ")
    > ' Check for invalid filename characters.
    > For Each invalidChar As Char In
    > System.IO.Path. GetInvalidFileN ameChars
    > strNewFileName = strNewFileName .Replace(invali dChar,
    > "")
    > Next
    >
    > Note that there is also a System.IO.Path. GetInvalidPathC hars function.
    > Not really sure what the difference is but I guess there must be some
    > characters that are allowed in the filename but not the folder name, or
    > vice-versa. Also, System.IO.Path. InvalidPathChar s has been replaced
    > by the two functions I mentioned above - you get an error if you try to
    > use System.IO.Path. InvalidPathChar s in VS2005.
    >
    >
    >
    > =============== =============== =============== =============== =======
    > From: Tom Shelton
    > Date: Thurs, Feb 10 2005 2:39 am
    > Email: Tom Shelton <tshel...@YOUKN OWTHEDRILLcomca st.net>
    > Groups: microsoft.publi c.dotnet.langua ges.vb
    >
    > On 2005-02-09, Nikolay Petrov <johntup2_nosp. ..@mail.bg> wrote:
    >[color=green]
    >> I need a way to strip chars from a string. The chars are all chars that
    >> are
    >> not allowed in file path.[/color]
    >[color=green]
    >> TIA[/color]
    >
    >
    > What ever way you do it - I would make sure that you get the list of
    > illegal chars from the System.IO.Path class's InvalidPathChar s
    > property.
    >
    > One way, would be to build a regular expression (as Herfried
    > suggested).
    > Or you could do it something like:
    >
    >
    > For Each invalidChar As Char In System.IO.Path. InvalidPathChar s
    > thePathString = thePathString.R eplace (invalidChar, vbNullChar)
    >
    > Next
    >
    >
    > HTH
    > --
    > Tom Shelton [MVP]
    >[/color]


    Comment

    • Cor Ligthert [MVP]

      #3
      Re: Strip chars from a string

      Imageanalyst.

      Please keep your replies to the original question, I will not be the only
      one who does not understand what you mean.

      Cor

      "ImageAnaly st" <hayworth@hotma il.com> schreef in bericht
      news:1146684271 .774048.256410@ v46g2000cwv.goo glegroups.com.. .[color=blue]
      > Tom, Nikolay:
      > That code doesn't work, at least not in VS2005. What happens is that
      > when you replace with VBNullChar, it basically chops off the string
      > from that point onwards. So Sna?*|fu" would become Sna instead of
      > Snafu. If you use two double quotes, then it works as you expect.
      > Here is corrected code:
      > strNewFileName = InputBox("Enter the new base file name: ")
      > ' Check for invalid filename characters.
      > For Each invalidChar As Char In
      > System.IO.Path. GetInvalidFileN ameChars
      > strNewFileName = strNewFileName .Replace(invali dChar,
      > "")
      > Next
      >
      > Note that there is also a System.IO.Path. GetInvalidPathC hars function.
      > Not really sure what the difference is but I guess there must be some
      > characters that are allowed in the filename but not the folder name, or
      > vice-versa. Also, System.IO.Path. InvalidPathChar s has been replaced
      > by the two functions I mentioned above - you get an error if you try to
      > use System.IO.Path. InvalidPathChar s in VS2005.
      >
      >
      >
      > =============== =============== =============== =============== =======
      > From: Tom Shelton
      > Date: Thurs, Feb 10 2005 2:39 am
      > Email: Tom Shelton <tshel...@YOUKN OWTHEDRILLcomca st.net>
      > Groups: microsoft.publi c.dotnet.langua ges.vb
      >
      > On 2005-02-09, Nikolay Petrov <johntup2_nosp. ..@mail.bg> wrote:
      >[color=green]
      >> I need a way to strip chars from a string. The chars are all chars that
      >> are
      >> not allowed in file path.[/color]
      >[color=green]
      >> TIA[/color]
      >
      >
      > What ever way you do it - I would make sure that you get the list of
      > illegal chars from the System.IO.Path class's InvalidPathChar s
      > property.
      >
      > One way, would be to build a regular expression (as Herfried
      > suggested).
      > Or you could do it something like:
      >
      >
      > For Each invalidChar As Char In System.IO.Path. InvalidPathChar s
      > thePathString = thePathString.R eplace (invalidChar, vbNullChar)
      >
      > Next
      >
      >
      > HTH
      > --
      > Tom Shelton [MVP]
      >[/color]


      Comment

      Working...