Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Hi,

I want to convert this function to VB.NET:

Private Function FilterFileName(ByVal FileName As String) As String
  FilterFileName = Empty
  Dim MyLoop As Integer
  For MyLoop = 1 To Len(FileName)
   If Asc(Mid$(FileName, MyLoop, 1)) >= 97 And Asc(Mid$(FileName, MyLoop, 1)) <= 122 Or _
      Asc(Mid$(FileName, MyLoop, 1)) >= 65 And Asc(Mid$(FileName, MyLoop, 1)) <= 90 Or _
      Asc(Mid$(FileName, MyLoop, 1)) >= 48 And Asc(Mid$(FileName, MyLoop, 1)) <= 57 Or _
      Asc(Mid$(FileName, MyLoop, 1)) = 32 Or _
      Asc(Mid$(FileName, MyLoop, 1)) = 45 Or _
      Asc(Mid$(FileName, MyLoop, 1)) = 95 Then
       FilterFileName = FilterFileName & Mid$(FileName, MyLoop, 1)
   End If
  Next
End Function

Everything is alright, I just cannot find the new Asc() method in .NET.

Each old method has a .NET version, for example InStr instead of Mid$ but where is new method for ASC()?

Thanks.

  • Leaders
Posted

Instead of the Mid() function, you can use the String.Chars property to get an individual character. Its quicker and returns a Char value, which you can compare with > and < without using the Asc() method.

Private Function FilterFileName(ByVal FileName As String) As String
  FilterFileName = Empty
  Dim MyLoop As Integer
  For MyLoop = 1 To Len(FileName)
   If FileName.Chars(MyLoop) >= "a"c And FileName.Chars(MyLoop) <= "z"c Or _
      FileName.Chars(MyLoop) >= "A"c And FileName.Chars(MyLoop) <= "Z"c Or _
      FileName.Chars(MyLoop) >= "0"c And FileName.Chars(MyLoop) <= "9"c Or _
      FileName.Chars(MyLoop) = " "c Or _
      FileName.Chars(MyLoop) = "-"c Or _
      FileName.Chars(MyLoop) = "_"c Then
       FilterFileName &= FileName.Chars(MyLoop)
   End If
  Next
End Function

You could make it even simpler, too, by using the functions of the Char type.

[Vb]

Private Function FilterFileName(ByVal FileName As String) As String

FilterFileName = Empty

Dim MyLoop As Integer

For MyLoop = 1 To Len(FileName)

Dim C As Char = FileName.Chars(MyLoop)

If Char.IsLetterOrDigit© Or _

C = " "c Or _

C = "-"c Or _

C = "_"c Then

FilterFileName &= FileName.Chars(MyLoop)

End If

Next

End Function[/code]

The System.IO.Path class has some functions that might make things simpler for you, too.

[sIGPIC]e[/sIGPIC]
Posted

You might also use RegEx

 

Imports System.Text.RegularExpressions;

Dim pattern as String = "^[a-zA-Z0-9_\s-]+$"
Dim expression as Regex = new Regex(pattern)

If expression.IsMatch(YourInputVariable) = True Then
   ' Valid file name
Else
   'Bad file name
End If

 

This will provide you with a more controlled file name than System.IO.Path methods will give you.

~Nate�

___________________________________________

Please use the [vb]/[cs] tags on posted code.

Please post solutions you find somewhere else.

Follow me on Twitter here.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...