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         }
原文地址:https://www.cnblogs.com/Scholars/p/9162598.html