轉自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文件所在的目錄)