我把路径保存在字符串str中,
例如:str = "c:\01\01\northwind.mdf"
请教用什么方法能取出 northwind .

解决方案 »

  1.   

    function GetFileName(byval fullpath as string ) as string 
    dim x as long 
    x =instrrev(fullpath,"\")
    if x then fullpath=mid(fullpath,x+1)
    x=instrrev(fullpath,".")
    if x>1 then  fullpath=left(fullpath,x-1)
    GetFileName=fullpath
    end function
    '未调试
      

  2.   

    Public Function GetFileName() as String
    dim iPos as Integer
    dim str as String,strTmp as string
    str = "c:\01\01\northwind.mdf"
    iPos=InstrRev(str,"\")   '最后一个\的位置
    if iPos<=0 then
    msgbox "文件名错误"
    exit function
    end if
    strTmp=Mid(str,iPos+1)   '文件名包含扩展名
    iPos=InstrRev(strTmp,".") '最后一个.的位置
    if iPos>1 Then strTmp=Left(strTmp,iPos-1)
    GetFileName=strTmp
    End Function
      

  3.   

    Public Function GetFileName() as String
    dim iPos as Integer
    dim str as String,strTmp as string
    str = "c:\01\01\northwind.mdf"
    iPos=InstrRev(str,"\")   '最后一个\的位置
    if iPos<=0 then
    msgbox "文件名错误"
    exit function
    end if
    strTmp=Mid(str,iPos+1)   '文件名包含扩展名
    iPos=InstrRev(strTmp,".") '最后一个.的位置
    if iPos>1 Then strTmp=Left(strTmp,iPos-1)
    GetFileName=strTmp
    End Function
      

  4.   

    Str = "c:\01\01\northwind.mdf"
    MsgBox Split(Mid(Str, InStrRev(Str, "\") + 1), ".")(0)
      

  5.   

    Private Function GetFileName(ByVal FullPath As String) As String
        FullPath = Dir(FullPath)
        GetFileName = Left(FullPath, InStr(FullPath, ".") - 1)
    End Function
    Private Sub Command1_Click()
        MsgBox GetFileName("c:\01\01\northwind.mdf")
    End Sub
      

  6.   

    northwolves(狼行天下) 的方法是最精的。
    不过变量名最好不要用VB的内置函数名,比如str。
      

  7.   

    谢谢各位!高手就是高手!To VBDN(PowerBASIC 中国 http://powerbasic.cn) 你的算法很好理解,可是调试不能通过,继续求解~
      

  8.   

    To northwolves(狼行天下)
    MsgBox Split(Mid(Str, InStrRev(Str, "\") + 1), ".")(0)
    为什么要在后面加上(0)?
      

  9.   

    问:为什么要在后面加上(0)?
    答:split函数将字符串转换成一维数组。比如数组s(),s(0)当然是返回数组的第一个元素了,s(1)返回第二个元素(在这个例子里将返回扩展名"mdf")。问:你的算法很好理解,可是调试不能通过,继续求解~
    答:"c:\01\01\northwind.mdf"这个路径在本机确实存在的情况下才能正常执行,所以,比狼行天下的代码差多了。