winform學習日誌(三十)----------從字串總分離檔案路徑、命名、副檔名,Substring(),LastIndexOf()的使用;替換某一類字串,Replace()的用法

來源:互聯網
上載者:User

標籤: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);        }    }}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.