Home All Groups Group Topic Archive Search About

Strip chars from a string

Author
3 May 2006 7:24 PM
ImageAnalyst
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.GetInvalidFileNameChars
                strNewFileName = strNewFileName .Replace(invalidChar,
"")
            Next

Note that there is also a System.IO.Path.GetInvalidPathChars 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.InvalidPathChars  has been replaced
by the two functions I mentioned above - you get an error if you try to
use System.IO.Path.InvalidPathChars  in VS2005.



===================================================================
From:  Tom Shelton
Date:  Thurs, Feb 10 2005 2:39 am
Email:   Tom Shelton <tshel***@YOUKNOWTHEDRILLcomcast.net>
Groups:   microsoft.public.dotnet.languages.vb

On 2005-02-09, Nikolay Petrov <johntup2_nosp***@mail.bg> wrote:

> I need a way to strip chars from a string. The chars are all chars that are
> not allowed in file path.

> TIA


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 InvalidPathChars
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.InvalidPathChars
        thePathString = thePathString.Replace (invalidChar, vbNullChar)

Next


HTH
--
Tom Shelton [MVP]

Author
3 May 2006 8:01 PM
vbnetdev
Function CleanInput(ByVal strIn As String) As String
        Return Regex.Replace(strIn, "[^\w\.@-]", "")
End Function



--
Get a powerful web, database, application, and email hosting with KJM
Solutions
http://www.kjmsolutions.com



Show quoteHide quote
"ImageAnalyst" <haywo***@hotmail.com> wrote in message
news:1146684271.774048.256410@v46g2000cwv.googlegroups.com...
> 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.GetInvalidFileNameChars
>                strNewFileName = strNewFileName .Replace(invalidChar,
> "")
>            Next
>
> Note that there is also a System.IO.Path.GetInvalidPathChars 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.InvalidPathChars  has been replaced
> by the two functions I mentioned above - you get an error if you try to
> use System.IO.Path.InvalidPathChars  in VS2005.
>
>
>
> ===================================================================
> From:  Tom Shelton
> Date:  Thurs, Feb 10 2005 2:39 am
> Email:   Tom Shelton <tshel***@YOUKNOWTHEDRILLcomcast.net>
> Groups:   microsoft.public.dotnet.languages.vb
>
> On 2005-02-09, Nikolay Petrov <johntup2_nosp***@mail.bg> wrote:
>
>> I need a way to strip chars from a string. The chars are all chars that
>> are
>> not allowed in file path.
>
>> TIA
>
>
> 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 InvalidPathChars
> 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.InvalidPathChars
>        thePathString = thePathString.Replace (invalidChar, vbNullChar)
>
> Next
>
>
> HTH
> --
> Tom Shelton [MVP]
>