C# 去字符串
来源:互联网 发布:人工智能能使死人复活 编辑:程序博客网 时间:2024/06/01 17:30
private void button4_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); }