zoukankan      html  css  js  c++  java
  • 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

            /////Readme
            /// <summary>
            /// 分隔符
            /// </summary>
            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;
            }
    
            /// <summary>
            /// 跳过的字符串
            /// </summary>
            private List<string> listPass = new List<string>();
            public void LoadPass()
            {
                string str = textBox3.Text.Trim();
                if (!string.IsNullOrWhiteSpace(str))
                {
                    listPass = str.Split(',').ToList();
                }
            }
    
            /// <summary>
            /// 小写转换为驼峰
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            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;
            }
    
            /// <summary>
            /// 字符拼装
            /// </summary>
            /// <param name="str"></param>
            /// <returns></returns>
            public string PieceString(string str)
            {
                StringBuilder strRes = new StringBuilder();
                LoadPass();
                if (!string.IsNullOrWhiteSpace(str))
                {
                    List<string> 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();
            }
    

      

  • 相关阅读:
    浅析String.intern()方法
    com.alibaba.druid.pool.DruidPooledConnection cannot be cast to oracle.jdbc.OracleConnection 异常解决办法
    ChannelEventRunnable handle RECEIVED operation error, channel is NettyChannel解决方法
    jsch channel is not opened原因及解决
    学习地址(oraclemysqllinux)
    python中数据分析常用函数整理
    Python之使用Pandas库实现MySQL数据库的读写
    Python数据分析
    如何用sql查询出连续三个月金额大于50的记录
    【Python项目实战】Pandas:让你像写SQL一样做数据分析(一)
  • 原文地址:https://www.cnblogs.com/JunGeHuang/p/10933802.html
Copyright © 2011-2022 走看看