vb.net vb中的子串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8943338/
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-09 15:53:29  来源:igfitidea点击:

Substring in vb

vb.netvisual-studio-2010

提问by beny lim

I am trying to perform a Substringfunction on a image filename. The name format is in "images.png".

我正在尝试对Substring图像文件名执行功能。名称格式为“ images.png”。

I tried using Substringit only allow me to indicate the first character till the "n" character to perform the function.

我尝试使用Substring它只允许我指示第一个字符直到“n”字符来执行该功能。

Such that SubString(1,6).

这样的SubString(1,6)

But what I want is to get any character before the ..

但我想要的是在..

For example "images.png":

例如“ images.png”:

After the Substringfunction I should get "images".

Substring函数之后我应该得到“图像”。

回答by Oded

You can use LastIndexOfin conjunction with Substring:

您可以LastIndexOf结合使用Substring

myString.Substring(0, myString.LastIndexOf('.'))

Though the Pathclass has a method that will do this in a strongly typed manner, whether the passed in path has directories or not:

尽管Path该类有一个以强类型方式执行此操作的方法,但传入的路径是否具有目录:

Path.GetFileNameWithoutExtension("images.png")

回答by Ash Burlaczenko

How about using the Pathclass.

如何使用Path类。

Path.GetFileNameWithoutExtension("filename.png");

回答by Peladao

In general for such string manipulations you can use:

通常,对于此类字符串操作,您可以使用:

mystring.Split("."c)(0)

But specifically for getting a filename without extension, it's best to use this method:

但特别是为了获取没有扩展名的文件名,最好使用这种方法:

System.IO.Path.GetFileNameWithoutExtension

System.IO.Path.GetFileNameWithoutExtension

回答by pingoo

Dim fileName As String = "images.png"

fileName = IO.Path.GetFileNameWithoutExtension(fileName)

Debug.WriteLine(fileName)

http://msdn.microsoft.com/en-us/library/system.io.path.getfilenamewithoutextension.aspx

http://msdn.microsoft.com/en-us/library/system.io.path.getfilenamewithoutextension.aspx

回答by shahkalpesh

string s = "images.png";
Console.WriteLine(s.Substring(0, s.IndexOf(".")));