转自https://www.cnblogs.com/dujinyang/p/4788028.htmlhtml
String.IndexOfthis
String.IndexOf 方法 (Char, Int32, Int32)
报告指定字符在此实例中的第一个匹配项的索引。搜索从指定字符位置开始,并检查指定数量的字符位置。
String.IndexOf(value, startIndex, count)
参数
value:要查找的 Unicode 字符。
startIndex:搜索起始位置。
count:要检查的字符位置数。
返回值(Int32):
若是找到该字符,则为 value 的索引位置;不然若是未找到,则为 -1。spa
名称 | 说明 | |
String.LastIndexOf (Char) | 报告指定 Unicode 字符在此实例中的最后一个匹配项的索引位置。 | |
String.LastIndexOf (String) | 报告指定的 String 在此实例内的最后一个匹配项的索引位置。 | |
String.LastIndexOf (Char, Int32) | 报告指定 Unicode 字符在此实例中的最后一个匹配项的索引位置。该搜索从指定字符位置开始。 | |
String.LastIndexOf (String, Int32) | 报告指定的 String 在此实例内的最后一个匹配项的索引位置。该搜索从指定字符位置开始。 | |
String.LastIndexOf (String, StringComparison) | 报告指定字符串在当前 String 对象中最后一个匹配项的索引。一个参数指定要用于指定字符串的搜索类型。 | |
String.LastIndexOf (Char, Int32, Int32) | 报告指定的 Unicode 字符在此实例内的子字符串中的最后一个匹配项的索引位置。搜索从指定字符位置开始,并检查指定数量的字符位置。 | |
String.LastIndexOf (String, Int32, Int32) | 报告指定的 String 在此实例内的最后一个匹配项的索引位置。搜索从指定字符位置开始,并检查指定数量的字符位置。 | |
String.LastIndexOf (String, Int32, StringComparison) |
|
|
String.LastIndexOf (String, Int32, Int32, StringComparison) |
|
示例:
string str = "深圳市盈基实业有限公司国际通邓事文*深圳市盈基实业有限公司国际通邓事文";
Label1.Text = str.LastIndexOf("邓文").ToString();//返回-1
Label1.Text = str.LastIndexOf("邓").ToString();//返回32
Label1.Text = str.LastIndexOf("邓",8).ToString();//返回-1
Label1.Text = str.LastIndexOf("邓",20).ToString();//返回14
Label1.Text = str.LastIndexOf("邓",33).ToString();//返回32
说明:在指定的范围内查找字符,这个范围是上面的输入的参数,理解为,从索引0开始到指定的数值位置范围内查找最后一个匹配的的字符串的位置。示例中,0-8中没有“邓”字,因此返回-1,0-20范围中,有一个“邓”字在索引14位置上,0-33范围中有两个“邓”字,由于LastIndexOf是返回最后一个匹配项索引位置,因此返32,而不是14。code
名称 | 说明 |
String.Substring (Int32) | 今后实例检索子字符串。子字符串从指定的字符位置开始。 |
String.Substring (Int32, Int32) | 今后实例检索子字符串。子字符串从指定的字符位置开始且具备指定的长度。 |
示例:
string str = "深圳市盈基实业有限公司国际通邓事文*深圳市盈基实业有限公司国际通邓事文";
Label1.Text = str.Substring(11);//返回 “国际通邓事文*深圳市盈基实业有限公司国际通邓事文”
Label1.Text = str.Substring(11,7);//返回 “国际通邓事文*”orm
Label1.Text = str.Substring(str.Length-3,3); // 返回邓事文,即截倒数3位字符htm
IndexOf、LastIndexOf都是返回一个位置,是个整数值;找不到都返回-1;
IndexOf是从左向右查,LastIndexOf是从右向左查,无论是IndexOf仍是LastIndexOf,索引序列都是从左到右的(起始值是0)
Substring是字符串截取,返回值是一个截取后的字符串。对象
C# 获取文件名及扩展名blog
string aFirstName = aFile.Substring(aFile.LastIndexOf("\\") + 1, (aFile.LastIndexOf(".") - aFile.LastIndexOf("\\") - 1)); //文件名
string aLastName = aFile.Substring(aFile.LastIndexOf(".") + 1, (aFile.Length - aFile.LastIndexOf(".") - 1)); //扩展名索引
string strFilePaht="文件路径";
Path.GetFileNameWithoutExtension(strFilePath);这个就是获取文件名的进程
还有的就是用Substring截取
strFilePaht.Substring(path.LastIndexOf("\\") + 1, path.Length - 1 - path.LastIndexOf("\\"));
strFilePaht.Substring(path.LastIndexOf("."), path.Length - path.LastIndexOf("."));
或者用openFileDialog1.SafeFileName
这样就能取到该文件的所在目录路径
string path1 = System.IO.Path.GetDirectoryName(openFileDialog1.FileName) + @"\";
string path = Path.GetFileName("C:\My Document\path\image.jpg"); //只获取文件名image.jpg
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
string fullPath = @"\WebSite1\Default.aspx";
string filename = System.IO.Path.GetFileName(fullPath);//文件名 “Default.aspx”
string extension = System.IO.Path.GetExtension(fullPath);//扩展名 “.aspx”
string fileNameWithoutExtension = System.IO.Path.GetFileNameWithoutExtension(fullPath);// 没有扩展名的文件名 “Default”
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
System.IO.Path.GetFileNam(filePath) //返回带扩展名的文件名
System.IO.Path.GetFileNameWithoutExtension(filePath) //返回不带扩展名的文件名
System.IO.Path.GetDirectoryName(filePath) //返回文件所在目录
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//获取当前进程的完整路径,包含文件名(进程名)。
string str = this.GetType().Assembly.Location;
result: X:\xxx\xxx\xxx.exe (.exe文件所在的目录+.exe文件名)
//获取新的 Process 组件并将其与当前活动的进程关联的主模块的完整路径,包含文件名(进程名)。
string str = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;
result: X:\xxx\xxx\xxx.exe (.exe文件所在的目录+.exe文件名)
//获取和设置当前目录(即该进程从中启动的目录)的彻底限定路径。
string str = System.Environment.CurrentDirectory;
result: X:\xxx\xxx (.exe文件所在的目录)
//获取当前 Thread 的当前应用程序域的基目录,它由程序集冲突解决程序用来探测程序集。
string str = System.AppDomain.CurrentDomain.BaseDirectory;
result: X:\xxx\xxx\ (.exe文件所在的目录+"\")
//获取和设置包含该应用程序的目录的名称。
string str = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
result: X:\xxx\xxx\ (.exe文件所在的目录+"\")
//获取启动了应用程序的可执行文件的路径,不包括可执行文件的名称。
string str = System.Windows.Forms.Application.StartupPath;
result: X:\xxx\xxx (.exe文件所在的目录)
//获取启动了应用程序的可执行文件的路径,包括可执行文件的名称。
string str = System.Windows.Forms.Application.ExecutablePath;
result: X:\xxx\xxx\xxx.exe (.exe文件所在的目录+.exe文件名)
//获取应用程序的当前工做目录(不可靠)。string str = System.IO.Directory.GetCurrentDirectory();result: X:\xxx\xxx (.exe文件所在的目录)