C# 字符串Trim进阶


private void button1_Click(object sender, EventArgs e)
        {//去掉字符串头尾指定字符
            string MyInfo= "--中华人民共和国--";
            //显示 "中华人民共和国"
            MessageBox.Show(MyInfo.Trim(new char[1] { '-' }), "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);            
            MyInfo = ",-中华人民共和国-,-";
            //显示 "中华人民共和国"
            MessageBox.Show(MyInfo.Trim(new char[2] { '-', ',' }), "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);                        
            MyInfo = "--中华人民共和国--";
            //显示 "中华人民共和国--"
            MessageBox.Show(MyInfo.TrimStart(new char[1] { '-' }), "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);                        
            MyInfo = ",-中华人民共和国-,-";
            //显示 "中华人民共和国-,-"
            MessageBox.Show(MyInfo.TrimStart(new char[2] { '-', ',' }), "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);                        
            MyInfo = "--中华人民共和国--";
            //显示 "--中华人民共和国"
            MessageBox.Show(MyInfo.TrimEnd(new char[1] { '-' }), "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);                        
            MyInfo = ",-中华人民共和国-,-";
            //显示 ",-中华人民共和国"
            MessageBox.Show(MyInfo.TrimEnd(new char[2] { '-', ',' }), "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);                        
        }

 

原文:https://www.cnblogs.com/Scholars/p/9162598.html

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM