Winform Learning Log (30) ---------- use of the path, name, extension, Substring (), and LastIndexOf () of the string in total separation; Replace () Usage

Source: Internet
Author: User

1. Separating the file path, name, and extension from the string,

Ii. 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 (openfilealodig1.showdidialog () = DialogResult. OK) // determine whether the file {string P_str_all = openFileDialog1.FileName is selected; // record the full path of the selected file string P_str_path = // obtain the file path P_str_all.Substring (0, p_str_all.LastIndexOf ("\") + 1 ); // start from the last/--- start from the last/the length of the last \ string P_str_filename = // get the file name P_str_all.Substring (P_str_all.LastIndexOf ("\") + 1, // start from the last/--- start from the last. the length of P_str_all.LastIndexOf (". ")-(P_str_all.LastIndexOf (" \ ") + 1); string P_str_fileexc = // get the file extension P_str_all.Substring (P_str_all.LastIndexOf (". ") + 1, // from the last one. start ---- from the last one. start the last length P_str_all.Length-P_str_all.LastIndexOf (". ")-1); lb_filepath.Text =" file path: "+ P_str_path; // display the file path lb_filename.Text =" file name: "+ P_str_filename; // display file name lb_fileexc.Text = "File Extension:" + P_str_fileexc; // display extension }}}}
3. replace a certain type of string,

4. 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 ReplaceString {public partial class Frm_Main: Form {public Frm_Main () {InitializeComponent ();} private void btn_replace_Click (object sender, EventArgs e) {txt_str.Text = // use the Reaplce method of the string object to replace all strings that meet the conditions txt_str.Text.Replace (txt_find.Text, txt_replace.Text );}}}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.