C# 移除字符串頭尾指定字符


 1  private void button1_Click(object sender, EventArgs e)
 2         {//去掉字符串頭尾指定字符
 3             string MyInfo= "--中華人民共和國--";
 4             //顯示 "中華人民共和國"
 5             MessageBox.Show(MyInfo.Trim(new char[1] { '-' }), "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);            
 6             MyInfo = ",-中華人民共和國-,-";
 7             //顯示 "中華人民共和國"
 8             MessageBox.Show(MyInfo.Trim(new char[2] { '-', ',' }), "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);                        
 9             MyInfo = "--中華人民共和國--";
10             //顯示 "中華人民共和國--"
11             MessageBox.Show(MyInfo.TrimStart(new char[1] { '-' }), "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);                        
12             MyInfo = ",-中華人民共和國-,-";
13             //顯示 "中華人民共和國-,-"
14             MessageBox.Show(MyInfo.TrimStart(new char[2] { '-', ',' }), "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);                        
15             MyInfo = "--中華人民共和國--";
16             //顯示 "--中華人民共和國"
17             MessageBox.Show(MyInfo.TrimEnd(new char[1] { '-' }), "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);                        
18             MyInfo = ",-中華人民共和國-,-";
19             //顯示 ",-中華人民共和國"
20             MessageBox.Show(MyInfo.TrimEnd(new char[2] { '-', ',' }), "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);                        
21         }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM