C#实现驼峰命名转下划线命名及反转

写在前面

实现方法不一定是最好的,只是给读者提供个思路。

准备

IDE:VisualStudio2019; 程序类型:WPF应用程序。

界面布局

首先简单做下界面,左边放一个ListBox存放输入的内容,中间放两个按钮,分别提供驼峰转下划线及下划线转驼峰功能。然后最右边放一个ListBox存放处理后的结果输出。

<Window x:Class="SomePersonTest.ConvertName"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:SomePersonTest"
        mc:Ignorable="d"
        Title="ConvertName" Height="450" Width="800">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="3*"></ColumnDefinition>
            <ColumnDefinition Width="*"></ColumnDefinition>
            <ColumnDefinition Width="3*"></ColumnDefinition>
        </Grid.ColumnDefinitions>

        <ListBox x:Name="lb_SourceData" Margin="10,10,10,10"
                 Grid.Column="0" FontSize="16">
        </ListBox>
        <StackPanel Orientation="Vertical" Margin="10,10,10,10"
                    Grid.Column="1">
            <Button x:Name="btn_ConvertTF" Margin="0,10,0,0"
                    Width="80" Height="30" FontSize="16"
                    Click="btn_ConvertTF_Click">转换驼峰</Button>
            <Button x:Name="btn_ConvertXHX" Margin="0,10,0,0"
                    Width="80" Height="30" FontSize="15"
                    Click="btn_ConvertXHX_Click">转换下划线</Button>
        </StackPanel>
        <ListBox x:Name="lb_TargetData" Margin="10,10,10,10"
                 Grid.Column="2" FontSize="16"></ListBox>
    </Grid>
</Window>

驼峰转下划线逻辑实现

首先遍历要处理的命名字符串,找到每一个大写字母,然后将其替换成下划线加相应的小写字母,最后拼接起来即可。

主要逻辑代码:

string strItemTarget = "";  //目标字符串
for (int j = 0; j < strItem.Length; j++)  //strItem是原始字符串
{
    string temp = strItem[j].ToString();
    if (Regex.IsMatch(temp, "[A-Z]"))
    {
        temp = "_" + temp.ToLower();
    }
    strItemTarget += temp;
}

下划线转驼峰逻辑实现

这里可以将要处理的字符串根据下划线符号分割成几段字符串,首段不处理,然后将其他段的首个字符转成大写字母,最后拼接起来即可。

主要逻辑代码:

string[] strItems = strItem.Split('_');
string strItemTarget = strItems[0];
for (int j = 1; j < strItems.Length; j++)
{
    string temp = strItems[j].ToString();
    string temp1 = temp[0].ToString().ToUpper();
    string temp2 = "";
    temp2 = temp1 + temp.Remove(0, 1);
    strItemTarget += temp2;
}

CS完整代码

ConvertName.xaml.cs的完整代码如下:

using System.Text.RegularExpressions;
using System.Windows;
using System.Windows.Controls;

namespace SomePersonTest
{
    /// <summary>
    /// ConvertName.xaml 的交互逻辑
    /// </summary>
    public partial class ConvertName : Window
    {
        public ConvertName()
        {
            InitializeComponent();
            InitData();
        }

        /// <summary>
        /// 转换驼峰
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_ConvertTF_Click(object sender, RoutedEventArgs e)
        {
            lb_TargetData.Items.Clear();
            for (int i = 0; i < lb_SourceData.Items.Count; i++)
            {
                string strItem = ((TextBox)(((ListBoxItem)lb_SourceData.Items[i]).Content)).Text.ToString();
                string[] strItems = strItem.Split('_');
                string strItemTarget = strItems[0];
                for (int j = 1; j < strItems.Length; j++)
                {
                    string temp = strItems[j].ToString();
                    string temp1 = temp[0].ToString().ToUpper();
                    string temp2 = "";
                    temp2 = temp1 + temp.Remove(0, 1);
                    strItemTarget += temp2;
                }
                lb_TargetData.Items.Add(strItemTarget);
            }
        }

        /// <summary>
        /// 转换下划线
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_ConvertXHX_Click(object sender, RoutedEventArgs e)
        {
            lb_TargetData.Items.Clear();
            for (int i = 0; i < lb_SourceData.Items.Count; i++)
            {
                string strItem = ((TextBox)(((ListBoxItem)lb_SourceData.Items[i]).Content)).Text.ToString();
                string strItemTarget = "";
                for (int j = 0; j < strItem.Length; j++)
                {
                    string temp = strItem[j].ToString();
                    if (Regex.IsMatch(temp, "[A-Z]"))
                    {
                        temp = "_" + temp.ToLower();
                    }
                    strItemTarget += temp;
                }
                lb_TargetData.Items.Add(strItemTarget);
            }
        }

        private void InitData()
        {
            for(int i = 0; i < 5; i++)
            { 
                ListBoxItem listBoxItem = new ListBoxItem();
                TextBox textBox = new TextBox();
                textBox.Width = 230;
                textBox.Height = 30;
                textBox.VerticalContentAlignment = VerticalAlignment.Center;
                listBoxItem.Content = textBox;
                lb_SourceData.Items.Add(listBoxItem);
            }
        }
    }
}

效果:

运行程序,先测一下驼峰转下划线,如图,看起来是没什么问题。
Test1
然后再来测测下划线转驼峰,如图,完美收工。
Test2

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

秦朝炼丹师

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值