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(); }