標籤:winform style blog class c code
一:從字串總分離檔案路徑、命名、副檔名,
二:代碼
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;namespace FilePathString{ public partial class Frm_Main : Form { public Frm_Main() { InitializeComponent(); } private void btn_Openfile_Click(object sender, EventArgs e) { if (openFileDialog1.ShowDialog() == DialogResult.OK)//判斷是否選擇了檔案 { string P_str_all = openFileDialog1.FileName;//記錄選擇的檔案全路徑 string P_str_path = //擷取檔案路徑 P_str_all.Substring(0, P_str_all.LastIndexOf("\\") + 1);//從最後一個/開始---到從最後一個/開始最後一個\的長度 string P_str_filename = //擷取檔案名稱 P_str_all.Substring(P_str_all.LastIndexOf("\\") + 1, //從最後一個/開始---到從最後一個/開始最後一個.的長度 P_str_all.LastIndexOf(".") - (P_str_all.LastIndexOf("\\")+1)); string P_str_fileexc = //擷取副檔名 P_str_all.Substring(P_str_all.LastIndexOf(".") + 1,//從最後一個.開始----到從最後一個.開始最後的長度 P_str_all.Length - P_str_all.LastIndexOf(".")-1); lb_filepath.Text = "檔案路徑: " + P_str_path;//顯示檔案路徑 lb_filename.Text = "檔案名稱: " + P_str_filename;//顯示檔案名稱 lb_fileexc.Text = "副檔名: " + P_str_fileexc;//顯示副檔名 } } }} 三:替換某一類字串,
四:代碼
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;namespace ReplaceString{ public partial class Frm_Main : Form { public Frm_Main() { InitializeComponent(); } private void btn_replace_Click(object sender, EventArgs e) { txt_str.Text = //使用字串對象的Reaplce方法替換所有滿足條件的字串 txt_str.Text.Replace(txt_find.Text, txt_replace.Text); } }}