VB.NET - Substring function that stops reading at first integer, possible?

时间:2015-05-12 23:05:48

标签: vb.net function substring

I currently have a text file that has a little over 500 lines of paths. (i.e., N:\Fork\Cli\Scripts\ABC01.VB)

Some of these file names vary in length (i.e., ABC01.VB, ABCDEF123.VB, etc) How can I go about using substring function to remove the path name, numbers, and file type, leaving just the letters.

For example, processing N:\Fork\Cli\Scripts\ABC01.VB, and returning ABC. Or N:\Fork\Cli\Scripts\ZUBDK22039.VB and returning ZUBDK.

I've only been able to retrieve the first 3 letters using this code

if (b==true)

1 个答案:

答案 0 :(得分:2)

As Plutonix points out, the best way to isolate the file name from a path is with sqlite> .tables list sqlite> .

You can extract just the letters (not digits or other characters) from a filename like this:

System.IO.Path.GetFileNameWithoutExtension

The above code sets Dim myPath As String = "N:\Fork\Cli\Scripts\AB42Cde01.VB" Dim filename As String = System.IO.Path.GetFileNameWithoutExtension(myPath) Dim letters As String = filename.Where(Function(c) Char.IsLetter(c)).ToArray to ABCde.

The code relies on the fact that Strings are treated like arrays of characters. The Where method processes all the characters in the string (array) and selects only the ones that are letters (using the Char.IsLetter method). The selected characters are converted to an array (string) that is assigned to the letters variable.

I see from your latest comment that it is not possible for numerals to be mixed with the letters (as in my example). However, the code should still work in your case.