Strip chars from a string

I

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 <[email protected]>
Groups: microsoft.public.dotnet.languages.vb

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


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
 
V

vbnetdev

Function CleanInput(ByVal strIn As String) As String
Return Regex.Replace(strIn, "[^\w\.@-]", "")
End Function
 
C

Cor Ligthert [MVP]

Imageanalyst.

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

Cor
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top