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