C#转换驼峰格式--首字母大写,可直接ctrl+v,支持跳过字符串

C#转换驼峰格式--首字母大写,可直接ctrl+v,支持跳过字符串

输入:2D_3D_23d_IB_REQUEST_OBSTACLES_GET_STATIC_OBJECTS_1_MESSAGE_STRUCT_SIZE_2D

结果:2D3D23dIBRequestObstaclesGetStaticO

环境 VS 2017

链接: https://pan.baidu.com/s/1nJ5bxynMhk9re6_8F-eFaA 提取码: wssy 

QQ交流:1023360745

C#转换驼峰格式--首字母大写,可直接ctrl+v,支持跳过字符串_第1张图片

 

        /////Readme
        /// 
        /// 分隔符
        /// 
        char charSplit = '_';
        //正则表达式在下面 其他格式自己调整一下

        private void button1_Click(object sender, EventArgs e)
        {
            string strConvert = textBox1.Text.Trim();

            //循环转换 并拼装字符串
            string strRes = PieceString(strConvert);

            //复制 直接粘贴即可
            Clipboard.SetDataObject(strRes);
            
            textBox2.Text = strRes;
            //清空输入方便快速操作
            textBox1.Text = string.Empty;
        }

        /// 
        /// 跳过的字符串
        /// 
        private List listPass = new List();
        public void LoadPass()
        {
            string str = textBox3.Text.Trim();
            if (!string.IsNullOrWhiteSpace(str))
            {
                listPass = str.Split(',').ToList();
            }
        }

        /// 
        /// 小写转换为驼峰
        /// 
        /// 
        /// 
        private string ConvertString(string tb1)
        {
            Match mt = Regex.Match(tb1, @"_(\w*)*");
            while (mt.Success)
            {
                var item = mt.Value;
                if (!string.IsNullOrWhiteSpace(item))
                {
                    if (listPass.Contains(item.ToLower()))
                    {
                        continue;
                    }
                }
                while (item.IndexOf('_') >= 0)
                {
                    string newUpper = item.Substring(item.IndexOf(charSplit), 2);
                    item = item.Replace(newUpper, newUpper.Trim(charSplit).ToUpper());
                    tb1 = tb1.Replace(newUpper, newUpper.Trim(charSplit).ToUpper());
                }
                mt = mt.NextMatch();
            }

            return tb1;
        }

        /// 
        /// 字符拼装
        /// 
        /// 
        /// 
        public string PieceString(string str)
        {
            StringBuilder strRes = new StringBuilder();
            LoadPass();
            if (!string.IsNullOrWhiteSpace(str))
            {
                List strList = str.Split(charSplit).ToList();
                foreach (var item in strList)
                {
                    if (listPass.Contains(item))
                    {
                        strRes.Append(item);
                    }
                    else
                    {
                        strRes.Append(ConvertString(charSplit + item.ToLower()));
                    }
                }
            }
            return strRes.ToString();
        }

  

 

转载于:https://www.cnblogs.com/JunGeHuang/p/10933802.html

你可能感兴趣的:(c#)