我想在图片文件名上执行一个Substring
函数。
文件名格式为"images.png"。
我尝试使用Substring
,但它只允许我指定从第一个字符到第n个字符来执行函数。
例如SubString(1,6)
。
但我想要的是获取.
前面的任何字符。
例如"images.png":
执行Substring
函数后,我应该得到"images"。
我想在图片文件名上执行一个Substring
函数。
文件名格式为"images.png"。
我尝试使用Substring
,但它只允许我指定从第一个字符到第n个字符来执行函数。
例如SubString(1,6)
。
但我想要的是获取.
前面的任何字符。
例如"images.png":
执行Substring
函数后,我应该得到"images"。
LastIndexOf
和 Substring
:myString.Substring(0, myString.LastIndexOf('.'))
虽然 Path
类有一种强类型的方法可以执行此操作,但无论传入的路径是否包含目录:
Path.GetFileNameWithoutExtension("images.png")
Path
类如何?Path.GetFileNameWithoutExtension("filename.png");
一般情况下,您可以使用以下方法进行字符串操作:
mystring.Split("."c)(0)
但是如果想要获取没有扩展名的文件名,最好使用这种方法:
System.IO.Path.GetFileNameWithoutExtension
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
string s = "images.png";
Console.WriteLine(s.Substring(0, s.IndexOf(".")));