WinForm Learning Log (30)----------The total separation of file paths, names, extensions, Substring (), LastIndexOf () from a string, substitution of a class of strings, replace () use

Source: Internet
Author: User

One: Always detach the file path, name, extension from the string,

Two: Code
usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Windows.Forms;namespacefilepathstring{ Public Partial classFrm_main:form { PublicFrm_main () {InitializeComponent (); }        Private voidBtn_openfile_click (Objectsender, EventArgs e) {            if(Openfiledialog1.showdialog () = = DialogResult.OK)//determine if a file is selected            {                stringP_str_all = Openfiledialog1.filename;//record the selected file full path                stringP_str_path =//Get file pathP_str_all. Substring (0, P_str_all. LastIndexOf ("\\") +1);//from the last/start---to the last \ length from the last/start                stringP_str_filename =//Get file nameP_str_all. Substring (P_str_all. LastIndexOf ("\\") +1,//from the last/start---to the last one from the last/start. LengthP_str_all. LastIndexOf (".") -(P_str_all. LastIndexOf ("\\")+1)); stringP_str_fileexc =//get file name extensionP_str_all. Substring (P_str_all. LastIndexOf (".") +1,//from the last one. Start----to from the last. Start the last lengthP_str_all. Length-p_str_all. LastIndexOf (".")-1); Lb_filepath. Text="file path:"+ P_str_path;//Show file pathLb_filename. Text ="file name:"+ P_str_filename;//Show file nameLb_fileexc. Text ="file extension:"+ P_str_fileexc;//Show Extensions            }        }    }}
Three: Replace a class of strings,

Four: Code
usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Windows.Forms;namespacereplacestring{ Public Partial classFrm_main:form { PublicFrm_main () {InitializeComponent (); }        Private voidBtn_replace_click (Objectsender, EventArgs e) {Txt_str. Text=//Replace all strings that meet the criteria using the Reaplce method of the string objectTxt_str. Text.replace (Txt_find. Text, Txt_replace.        Text); }    }}

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.