• Java驼峰和下划线互相转化


    直接上代码 :

    package com.utils;
    
    
    public class ChangeChar {
    
        public static final char UNDERLINE = '_';
    
        public static void main(String[] args) {
    
             /*驼峰转下划线*/
            String str = "   itemName    bbbbCsss     ,  
    " +
                    "    category     ,
    " +
                    "	barCode	 
    " +
                    "	volume	  
    " +
                    "	length	  
    " +
                    "	width	 
    " +
                    "	height	 
    " +
                    "	unitPrice	 
    " +
                    "	shelfLife	 
    " +
                    "	shelfLifeUnit	  /
    " +
                    "	 )";
    
            /*下划线转驼峰*/
            String str2 = " ITEM_NAME         ,  
    " +
                    "    CATEGORY     ,
    " +
                    "	BAR_CODE	 
    " +
                    "	VOLUME	  
    " +
                    "	LENGTH	  
    " +
                    "	WIDTH	 
    " +
                    "	HEIGHT	 
    " +
                    "	UNIT_PRICE	 
    " +
                    "	SHELF_LIFE	 
    " +
                    "	SHELF_LIFE_UNIT	";
    
            /**
             * 测试
             * */
             /* charType=2 表示大写, 其他情况都是小写*/
            String STR_ABC = camelToUnderline(str, 2);   //  下划线大写:ABC_DEF
            String str_abc = camelToUnderline(str, 1);   //  下划线小写:abc_def
            System.out.println("驼峰转化成下划线大写 :" + STR_ABC);
            System.out.println("驼峰转化成下划线小写 :" + str_abc);
    
            String strAbc = underlineToCamel(str2);   //  下划线转驼峰:abcDef
            System.out.println("下划线化成驼峰 :" + strAbc);
    
        }
    
    
        //驼峰转下划线
        public static String camelToUnderline(String param, Integer charType) {
            if (param == null || "".equals(param.trim())) {
                return "";
            }
            int len = param.length();
            StringBuilder sb = new StringBuilder(len);
            for (int i = 0; i < len; i++) {
                char c = param.charAt(i);
                if (Character.isUpperCase(c)) {
                    sb.append(UNDERLINE);
                }
                if (charType == 2) {
                    sb.append(Character.toUpperCase(c));  //统一都转大写
                } else {
                    sb.append(Character.toLowerCase(c));  //统一都转小写
                }
    
    
            }
            return sb.toString();
        }
    
        //下划线转驼峰
        public static String underlineToCamel(String param) {
            if (param == null || "".equals(param.trim())) {
                return "";
            }
            int len = param.length();
            StringBuilder sb = new StringBuilder(len);
            Boolean flag = false; // "_" 后转大写标志,默认字符前面没有"_"
            for (int i = 0; i < len; i++) {
                char c = param.charAt(i);
                if (c == UNDERLINE) {
                    flag = true;
                    continue;   //标志设置为true,跳过
                } else {
                    if (flag == true) {
                        //表示当前字符前面是"_" ,当前字符转大写
                        sb.append(Character.toUpperCase(param.charAt(i)));
                        flag = false;  //重置标识
                    } else {
                        sb.append(Character.toLowerCase(param.charAt(i)));
                    }
                }
            }
            return sb.toString();
        }
    
    
    }
  • 相关阅读:
    四、元数据ParameterMetaData
    三、PreparedStatement对象(重点)
    二、properties文件存储信息
    一、JDBC3.0标准中常用接口与类
    十一、MySQL 数据库存储引擎介绍
    十、执行计划
    注册表中一些项目拒绝访问,拒绝更改权限,拒绝删除
    x64dbg 的条件断点及断点日志
    手机APP无法抓包HTTPS解决方案
    原版黑苹果安装(个人备忘)
  • 原文地址:https://www.cnblogs.com/coloz/p/10911174.html
Copyright © 2020-2023  润新知