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);
}
}
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://blog.csdn.net/sunyuhua_keyboard/article/details/121533130
内容来源于网络,如有侵权,请联系作者删除!