java将驼峰命令转化为下滑下划线的方法

x33g5p2x  于2021-11-25 转载在 Java  
字(1.0k)|赞(0)|评价(0)|浏览(475)
package com.fastone.www.javademo;

import java.util.ArrayList;
import java.util.List;

/**
 * @program: javademo
 * @description: 将驼峰命令转化为下滑下划线的方法
 * @author: sunyuhua
 * @create: 2021-11-24 21:33
 **/
public class TestCamel2Sanke {

	public static void main(String[] args) {
		TestCamel2Sanke testCamel2Sanke=new TestCamel2Sanke();
		testCamel2Sanke.camel2Sanke("createAt");
	}

	public void camel2Sanke(String camelStr){
		List<String> camelStrArray = new ArrayList();
		int start = 0;
		for(char ch : camelStr.toCharArray()){
			if(ch >= 'A' && ch <= 'Z') {
				String tempStr = camelStr.substring(start, camelStr.indexOf(ch));
				tempStr = tempStr.toLowerCase();
				camelStrArray.add(tempStr);
				start = camelStr.indexOf(ch);
			}
		}
		//最后一个单词的处理
		camelStrArray.add(camelStr.substring(start,camelStr.length()).toLowerCase());

		StringBuilder stringBuilder = new StringBuilder();
		for(String chs : camelStrArray){
			stringBuilder.append(chs + "_");
		}
		String result = stringBuilder.toString();
		if(result.lastIndexOf("_") > 0){
			result = result.substring(0,result.length()-1);
		}
		System.out.println(result);
	}

}

相关文章

最新文章

更多