轉自https://www.cnblogs.com/dujinyang/p/4788028.html
String.IndexOf
String.IndexOf 方法 (Char, Int32, Int32)
報告指定字符在此實例中的第一個匹配項的索引。搜索從指定字符位置開始,並檢查指定數量的字符位置。
String.IndexOf(value, startIndex, count)
參數
value:要查找的 Unicode 字符。
startIndex:搜索起始位置。
count:要檢查的字符位置數。
返回值(Int32):
如果找到該字符,則為 value 的索引位置;否則如果未找到,則為 -1。
String.LastIndexOf
名稱 | 說明 | |
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。
String.Substring
名稱 | 說明 |
String.Substring (Int32) | 從此實例檢索子字符串。子字符串從指定的字符位置開始。 |
String.Substring (Int32, Int32) | 從此實例檢索子字符串。子字符串從指定的字符位置開始且具有指定的長度。 |
示例:
string str = "深圳市盈基實業有限公司國際通鄧事文*深圳市盈基實業有限公司國際通鄧事文";
Label1.Text = str.Substring(11);//返回 “國際通鄧事文*深圳市盈基實業有限公司國際通鄧事文”
Label1.Text = str.Substring(11,7);//返回 “國際通鄧事文*”
Label1.Text = str.Substring(str.Length-3,3); // 返回鄧事文,即截倒數3位字符
總結:
IndexOf、LastIndexOf都是返回一個位置,是個整數值;找不到都返回-1;
IndexOf是從左向右查,LastIndexOf是從右向左查,不管是IndexOf還是LastIndexOf,索引序列都是從左到右的(起始值是0)
Substring是字符串截取,返回值是一個截取后的字符串。
OpenFileDialog控件的基本屬性
- InitialDirectory:對話框的初始目錄
- Filter: 獲取或設置當前文件名篩選器字符串,例如,"文本文件(*.txt)|*.txt|所有文件(*.*)||*.*"
- FilterIndex 在對話框中選擇的文件篩選器的索引,如果選第一項就設為1
- RestoreDirectory 控制對話框在關閉之前是否恢復當前目錄
- FileName:第一個在對話框中顯示的文件或最后一個選取的文件
- Title 將顯示在對話框標題欄中的字符
- AddExtension 是否自動添加默認擴展名
- CheckPathExists 在對話框返回之前,檢查指定路徑是否存在
- DefaultExt 默認擴展名
- DereferenceLinks 在從對話框返回前是否取消引用快捷方式
- ShowHelp 啟用"幫助"按鈕
-
-
C# 獲取文件名及擴展名
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文件所在的目錄)