private void btnStart_Click(object sender, EventArgs e) { var srcWords = ToDBC(txtSrc.Text.Trim()); var placeStr = Regex.Replace(srcWords, @"([\p{P}*])", " "); placeStr = Regex.Replace(placeStr,@"([\u4e00-\u9fa5])", ""); var splitWords = placeStr.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries); var words = splitWords.ToList().Select(item => { if (item.Length > 0) { var first = item.Substring(0, 1).ToUpper(); var last = ""; if (item.Length > 2) { last = item.Substring(1, item.Length-1); } return first + last; } return ""; }); var joinWords = string.Join(" ", words); Clipboard.SetText(joinWords); txtDes.Text = AppendWords(joinWords); MessageBox.Show("已将替换后的字符串复制到粘贴板"); } /// <summary> /// 替换所有的中文及标点符号 /// </summary> /// <param name="str"></param> /// <returns></returns> string RemoveAllChineseAndSymbol(string str) { var placeStr = Regex.Replace(str, @"([\p{P}*])", " "); placeStr = Regex.Replace(placeStr, @"([\u4e00-\u9fa5])", ""); return placeStr; } /// <summary> /// 追加及替换内容 /// </summary> /// <param name="str"></param> /// <returns></returns> string AppendWords(string str) { var append = Regex.Replace(ToDBC(txtAppend.Text.Trim()), @"[\p{P}*]", " "); if (string.IsNullOrEmpty(append)) { return str; } var i=combSelect.SelectedIndex; if (i==0) { return str.Trim() + " " + append; }else if (i==1) { return append + " " + str.Trim(); }else if (i==2) { var replaceTo=Regex.Replace(ToDBC(txtToReplace.Text.Trim()), @"[\p{P}*]", " "); return str.Trim().Replace(append, replaceTo); } else { return str; } } /// <summary> /// 全角转半角 /// </summary> /// <param name="input"></param> /// <returns></returns> public string ToDBC(string input) { char[] c = input.ToCharArray(); for (int i = 0; i < c.Length; i++) { if (c[i] == 12288) { c[i] = (char)32; continue; } if (c[i] > 65280 && c[i] < 65375) c[i] = (char)(c[i] - 65248); } return new String(c); } private void combSelect_SelectedIndexChanged(object sender, EventArgs e) { var cb=(ComboBox) sender; txtToReplace.Visible = cb.SelectedIndex==2; }
时间: 2024-10-13 20:56:52