我有一个文本框,它的text属性就是这样的路径,例如:
/users/me/whatever
我想要的是删除最后一个“文件夹”并拥有:
/users/me
我尝试过修剪和分割功能,但是现在我没有达到我想要的效果。
编辑:
Private Sub btn_Back_Click(sender As Object, e As EventArgs) Handles btn_Back.Click
If Not (txt_Path.Text = "/") Then
txt_Path.Text = ... ?
End If
End Sub
有人可以帮忙吗?感谢
答案 0 :(得分:2)
txt_Path.Text = Path.GetDirectoryName(txt_Path.Text)
答案 1 :(得分:2)
这很简单:
Dim str = txt_Path.Text
Dim i = str.LastIndexOf("/")
If i <> -1 then
txt_Path.Text = str.Substring(0, i)
End if
答案 2 :(得分:0)
您也可以使用regular expression:
解析此问题Dim dir As String
' this may not work as expected if a user ends the directory with as slash, try it to see and then decide if that is ok or not?
' this regex pattern keeps everything except "everything that follows the last slash"
dir = Regex.Replace("/users/me/whatever", "^(.*)\/.*[^\/]$", "$1")
Console.WriteLine(dir)