java:驼峰转下划线

时间:2025-02-16 08:40:03
package com.xiaoxu.crawler.utils; import com.google.common.collect.Lists; import com.xiaoxu.crawler.excp.CrawlerForJException; import org.apache.commons.lang3.ArrayUtils; import org.springframework.util.CollectionUtils; import org.springframework.util.StringUtils; import java.text.MessageFormat; import java.util.List; /** * @author xiaoxu * @date 2022-11-19 21:52 * crawlerJ: */ public class StrUtils { private static final String underLineMark = "_"; private static final String EMPTY_STRING = ""; public static boolean equals(String a, String b){ if(null == a){ return null == b; } return a.equals(b); } public static int getMatchCount(String str, String subStr){ if(!StringUtils.hasLength(str) || !StringUtils.hasLength(subStr)){ ExcpUtils.throwExp( MessageFormat.format("getMatchCount's str and subStr should not be null or empty:{0},{1}.", str, subStr)); } return StringUtils.countOccurrencesOf(str, subStr); } /** * @param name 小驼峰命名Str * @return 下划线 */ @SuppressWarnings("all") public static String humpTransferUnderline(String name){ // null or empty throw exp ExcpUtils.throwExpIfFalse(StringUtils.hasLength(name), "when hump transfer to underline, name should not be empty."); CharSequence cs = name; List<CharSequence> charSequenceList = Lists.newArrayList(); int temI = 0, i = 0, csLen = 0; for (; i < (csLen = cs.length()); i++) { char c = cs.charAt(i); if(Character.isUpperCase(c)){ CharSequence csq = cs.subSequence(temI, i); if(csq.length() > 0){ addCharSequence(charSequenceList, csq); temI = i; } } } CharSequence lastSequence = cs.subSequence(temI, csLen); if(lastSequence.length() > 0){ addCharSequence(charSequenceList, lastSequence); } // actual could not execute this if(CollectionUtils.isEmpty(charSequenceList)) return EMPTY_STRING; return String.join(underLineMark, charSequenceList); } private static void addCharSequence(List<CharSequence> charSequenceList, CharSequence charSequence) { if(null == charSequenceList){ throw new CrawlerForJException("charSequenceList could not be null"); } if(null == charSequence || charSequence.length() <= 0){ throw new CrawlerForJException("charSequence need non empty"); } char[] csqChars = charSequence.toString().toCharArray(); char[] initialLowerCsqChar = new char[csqChars.length]; initialLowerTransfer(initialLowerCsqChar, csqChars); charSequenceList.add(new String(initialLowerCsqChar)); } private static void initialLowerTransfer(char[] targetChar, char[] originChar){ ExcpUtils.throwExpIfFalse(ArrayUtils.isNotEmpty(targetChar), "targetChar is empty"); ExcpUtils.throwExpIfFalse(ArrayUtils.isNotEmpty(originChar), "originChar is empty"); int totalLength; ExcpUtils.throwExpIfFalse((totalLength = originChar.length) == targetChar.length, "targetChar'length not equals to originChar's length"); char[] temp;int tempSize; System.arraycopy((temp = new char[]{Character.toLowerCase(originChar[0])}), 0 , targetChar, 0, (tempSize = temp.length)); if(totalLength > tempSize){ System.arraycopy(originChar, tempSize, targetChar, tempSize, totalLength - tempSize); } } public static void main(String[] args) { System.out.println(humpTransferUnderline("a")); System.out.println(humpTransferUnderline("A")); System.out.println(humpTransferUnderline("age")); System.out.println(humpTransferUnderline("Age")); System.out.println(humpTransferUnderline("ageABBadA")); System.out.println(humpTransferUnderline("AddressFrom")); System.out.println(humpTransferUnderline("gmt_create")); System.out.println(humpTransferUnderline("gmtCreate")); } }