vba VB 无法获得正确的函数来处理字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11111276/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 16:32:22  来源:igfitidea点击:

VB can't get the right function to work with strings

vb.netvisual-studio-2010vba

提问by Pharap

Basically, I'm trying to do some string manipulation to edit directories. I found some code to try and edit the directories, but when I use it it doesn't recognise 'right' as being a function and only recognises it as a right property, thus producing an error.

基本上,我正在尝试进行一些字符串操作来编辑目录。我找到了一些代码来尝试编辑目录,但是当我使用它时,它不会将“正确”识别为函数,而只会将其识别为正确的属性,从而产生错误。

I was wondering if there's something I haven't imported or if perhaps 'right' is an obsolete function that was used in VB6 but replaced with something.

我想知道是否有我没有导入的东西,或者“正确”是否是在 VB6 中使用但被某些东西替换的过时函数。

The code I have is as follows:

我的代码如下:

Dim Foo As String
Dim Bar As String
Bar = 'some form of directory input i.e. my.computer.currentdirectory
Foo = right(Bar, (Len(Bar) - InStrRev(Bar, "/")))
MsgBox(Foo)

Ideally I need either a better method of manipulating directories or a way to get the 'right' functionality working.

理想情况下,我需要一种更好的目录操作方法或一种使“正确”功能正常工作的方法。

回答by Reed Copsey

but when I use it it doesn't recognise 'right' as being a function and only recognises it as a right property, thus producing an error.

但是当我使用它时,它不会将“正确”识别为函数,而只会将其识别为正确的属性,从而产生错误。

If you have a "right" property, you can fully qualify the function:

如果您有“正确”的属性,则可以完全限定该功能:

Foo = Microsoft.VisualBasic.Right(Bar, (Len(Bar) - InStrRev(Bar, "/")))

For details, see the docs for the Right Function.

有关详细信息,请参阅Right Function的文档。

Note that, for directory parsing, you can handle this much more cleanly via the System.IOnamespace. In particular, you can construct a DirectoryInfoand get the parent folder via the Parentproperty.

请注意,对于目录解析,您可以通过System.IO命名空间更干净地处理它。特别是,您可以构造DirectoryInfo并通过Parent属性获取父文件夹。

You can also use Path.GetDirectoryNameto work with strings. In your case, if you had Barset to "C:\Some\Path\To\A\File.txt" and you call Path.GetDirectoryName(Bar), it will return "C:\Some\Path\To\A". If you call it on that, you'll get ""C:\Some\Path\To", etc.

您还可以使用Path.GetDirectoryName来处理字符串。在您的情况下,如果您已Bar设置为“C:\Some\Path\To\A\File.txt”并调用Path.GetDirectoryName(Bar),它将返回“C:\Some\Path\To\A”。如果你调用它,你会得到 ""C:\Some\Path\To" 等。

回答by Rich

Look up System.IO.Path - has lots of useful tools for this stuff. You'll want GetDirectoryName and GetFileName especially. They work on directories as well as filenames.

查找 System.IO.Path - 有很多用于这些东西的有用工具。您将尤其需要 GetDirectoryName 和 GetFileName。它们处理目录和文件名。

Bar = "C:\Dir1\Dir2\Dir3"
Foo = IO.Path.GetFileName(Bar)  'now = Dir3
Foo = IO.Path.GetDirectoryName(Bar)  'now = C:\Dir1\Dir2

http://msdn.microsoft.com/en-us/library/system.io.path.getfilename
http://msdn.microsoft.com/en-us/library/system.io.path.getdirectoryname

http://msdn.microsoft.com/en-us/library/system.io.path.getfilename
http://msdn.microsoft.com/en-us/library/system.io.path.getdirectoryname