zoukankan      html  css  js  c++  java
  • c# 替换所有中文、标点符号,全角转半角

         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;
            }
  • 相关阅读:
    iOS 中Block以及Blocks的使用,闭包方法调用
    iOS 网络监听、判断
    微信摇一摇实现原理,视图展示
    iOS cocos2d安装以及问题解决
    iOS cocos2d游戏引擎的了解之一
    Leetcode-Valid Sudoku
    Leetcode-Count and Say
    Leetcode-Length of Last Word
    Leetcode-Merge Two Sorted Lists
    Leetcode-Add Binary
  • 原文地址:https://www.cnblogs.com/huang361964533/p/5130310.html
Copyright © 2011-2022 走看看