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
Substring in vb
提问by beny lim
I am trying to perform a Substring
function on a image filename.
The name format is in "images.png".
我正在尝试对Substring
图像文件名执行功能。名称格式为“ images.png”。
I tried using Substring
it 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 Substring
function I should get "images".
在Substring
函数之后我应该得到“图像”。
回答by Oded
You can use LastIndexOf
in conjunction with Substring
:
您可以LastIndexOf
结合使用Substring
:
myString.Substring(0, myString.LastIndexOf('.'))
Though the Path
class 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 Path
class.
如何使用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(".")));