将带下杠的数据库字段名转换成驼峰式的变量名

//将带下杠的数据库字段名转换成驼峰式的变量名;以及将驼峰式的变量名转换成带下杠的数据库字段名

	// 输入 = last_login_time   输出 = lastLoginTime
	public static String toNoBlank( String param ) {
		StringBuffer sbf = new StringBuffer();

		if ( param.contains( "_" ) ) {
	         String[] arr = param.split( "_" );


	         for ( int i = 0; i < arr.length; i++ ) {
	             if ( i == 0 ) {
	                  sbf.append( arr[ i ] );

	                  continue;
	             }

	             sbf.append( ( arr[ i ].charAt( 0 ) + "" ).toUpperCase() 
	            		 + arr[ i ].substring( 1, arr[ i ].length() ) );
	         }
	         return sbf.toString();
	    } else {
	    	return param;
	    }
	}
		
	// 输入 = lastLoginTime    输出 = last_login_time
	public static String withBlank( String input2 ) {
		StringBuffer sbf = new StringBuffer();
		
		int tempPos = 0;

		for ( int i = 0; i < input2.length(); i++ ) {
			char a = input2.charAt( i );
			
			if ( ( int ) a >= ( int ) 'A' == true 
                                      && ( int ) a <= ( int ) 'Z' == true ) {
				sbf.append( input2.substring( tempPos, i ).toLowerCase() + "_" );
				
				tempPos = i;
			}
		}
		
		sbf.append( input2.substring( tempPos, input2.length() ).toLowerCase() );
		
		return sbf.toString();
	}

	// main
	public static void main( String[] param ) {
		String input = "last_login_time";
		String re = toNoBlank( input );
		System.out.println( re );     // 输入last_login_time,输出lastLoginTime

		String input2 = "lastLoginTime";
		String re2 = withBlank( input2 );
		System.out.println( re2 );    // 输入lastLoginTime,输出last_login_time 
	}

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值