Vb.net 从上的特定字符修剪字符串

Vb.net 从上的特定字符修剪字符串,vb.net,string,trim,Vb.net,String,Trim,我有一个文本框,它的文本属性是这样的路径,例如: /users/me/whatever 我想要的是删除最后一个“文件夹”,并具有以下内容: /users/me 我尝试过修剪和分割功能,但现在我并没有达到我想要的 编辑: 有人能帮忙吗?谢谢你可以试试 你可以试试 这很简单: Dim str = txt_Path.Text Dim i = str.LastIndexOf("/") If i <> -1 then txt_Path.Text = str.Substring(0,

我有一个文本框,它的文本属性是这样的路径,例如:

/users/me/whatever
我想要的是删除最后一个“文件夹”,并具有以下内容:

/users/me
我尝试过修剪和分割功能,但现在我并没有达到我想要的

编辑:

有人能帮忙吗?谢谢你可以试试

你可以试试

这很简单:

Dim str = txt_Path.Text
Dim i = str.LastIndexOf("/")
If i <> -1 then
   txt_Path.Text = str.Substring(0, i)
End if
Dim str=txt\u Path.Text
尺寸i=str.LastIndexOf(“/”)
如果i-1那么
txt_Path.Text=str.Substring(0,i)
如果结束
这很简单:

Dim str = txt_Path.Text
Dim i = str.LastIndexOf("/")
If i <> -1 then
   txt_Path.Text = str.Substring(0, i)
End if
Dim str=txt\u Path.Text
尺寸i=str.LastIndexOf(“/”)
如果i-1那么
txt_Path.Text=str.Substring(0,i)
如果结束

您还可以使用以下方法分析此问题:


您还可以使用以下方法对此进行分析:


在/上拆分,string.LastIndexOf。。Regex提供了很多方法来实现这一点。没有代码,所以我不知道你为什么在挣扎。只需将代码插入我的postSplit on/,string.LastIndexOf。。Regex提供了很多方法来实现这一点。没有代码,所以我不知道你为什么在挣扎。只是在我的博文中插入了代码谢谢,这不完全是我想要的,但它将不得不这样做now@chiapa你在找什么?哦,别误会,结果正是我所需要的,但我希望找到一个字符串函数,可以删除特定字符上的所有内容,就像我问题的标题一样@chiapa我认为没有内置这样的函数,但是如果需要在其他地方重用,可以使用上面的代码轻松地为string类创建自己的扩展函数。是的,我知道。再次感谢汉克斯,这不完全是我想要的,但它必须为你做now@chiapa你在找什么?哦,别误会,结果正是我所需要的,但我希望找到一些字符串函数,可以删除特定字符上的所有内容,如我问题的标题所示@chiapa我认为没有内置这样的函数,但是如果需要在其他地方重用,可以使用上面的代码轻松地为string类创建自己的扩展函数。是的,我知道。再次感谢
Dim str = txt_Path.Text
Dim i = str.LastIndexOf("/")
If i <> -1 then
   txt_Path.Text = str.Substring(0, i)
End if
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)