trim 最后一个斜线之前的所有内容 vb.net
trim everything before last slash vb.net
昨天我学会了 trim 使用流动代码
文件位置的文件名
Dim NEWPATH As String = (inventorApp.ActiveDocument.FullFileName)
NEWPATH = NEWPATH.Substring(0, NEWPATH.fIndexOf("\"c))
这真的很简洁,因为它比我以前使用的方法更可靠/稳定...哈哈,今天虽然我想要相反的我想 trim 最后一个斜线之前的所有内容我怎么能这样做吗?
同样出于好奇,("\"c) 中的小写字母 c 是什么意思,即代码在没有它的情况下也能正常工作?
您可以使用 Path
class:
的方法,而不是摆弄子字符串
Dim fullpath as String = inventorApp.ActiveDocument.FullFileName
'What you're after now - the filename
Dim justTheFileName as String = Path.GetFileName(fullpath)
'a replacement for what you're already doing to get the folder name
Dim justTheFolderName as String = Path.GetDirectoryName(fullpath)
"\"c
中的小写 c
表示您想要 Char
而不是 String
,这也是 this particular overload of IndexOf
takes, but there's a String
overload,所以它会没有 c
.
也能正常工作
您可以使用 split() 函数或这个
Dim mystr As String = "Dr. John Smith 123 Main Street 12345"
Dim cut_at As String = "Smith"
Dim x As Integer = InStr(mystr, cut_at)
Dim string_before As String = mystr.Substring(0, x - 2)
Dim string_after As String = mystr.Substring(x + cut_at.Length-1)
一个解决方案是使用 LastIndexOf()
创建一个仅包含最后一个斜杠之后的内容的子字符串:
Dim fullpath As String = inventorApp.ActiveDocument.FullFileName
Dim FileName As String = fullPath.Substring(fullPath.LastIndexOf("\"))
我喜欢这个,因为它更通用(即您可以通过将 "\"
替换为 "."
来恢复文件扩展名)。但是,如果您确定您正在使用文件路径,请使用 James Thorpe 的解决方案,使用 System.IO.Path
昨天我学会了 trim 使用流动代码
文件位置的文件名 Dim NEWPATH As String = (inventorApp.ActiveDocument.FullFileName)
NEWPATH = NEWPATH.Substring(0, NEWPATH.fIndexOf("\"c))
这真的很简洁,因为它比我以前使用的方法更可靠/稳定...哈哈,今天虽然我想要相反的我想 trim 最后一个斜线之前的所有内容我怎么能这样做吗?
同样出于好奇,("\"c) 中的小写字母 c 是什么意思,即代码在没有它的情况下也能正常工作?
您可以使用 Path
class:
Dim fullpath as String = inventorApp.ActiveDocument.FullFileName
'What you're after now - the filename
Dim justTheFileName as String = Path.GetFileName(fullpath)
'a replacement for what you're already doing to get the folder name
Dim justTheFolderName as String = Path.GetDirectoryName(fullpath)
"\"c
中的小写 c
表示您想要 Char
而不是 String
,这也是 this particular overload of IndexOf
takes, but there's a String
overload,所以它会没有 c
.
您可以使用 split() 函数或这个
Dim mystr As String = "Dr. John Smith 123 Main Street 12345"
Dim cut_at As String = "Smith"
Dim x As Integer = InStr(mystr, cut_at)
Dim string_before As String = mystr.Substring(0, x - 2)
Dim string_after As String = mystr.Substring(x + cut_at.Length-1)
一个解决方案是使用 LastIndexOf()
创建一个仅包含最后一个斜杠之后的内容的子字符串:
Dim fullpath As String = inventorApp.ActiveDocument.FullFileName
Dim FileName As String = fullPath.Substring(fullPath.LastIndexOf("\"))
我喜欢这个,因为它更通用(即您可以通过将 "\"
替换为 "."
来恢复文件扩展名)。但是,如果您确定您正在使用文件路径,请使用 James Thorpe 的解决方案,使用 System.IO.Path