feat(CharSequenceUtil): 提供大/小写下标字母

This commit is contained in:
topsuder 2023-12-05 11:50:04 +08:00
parent 7123979bea
commit cf182e7637
2 changed files with 99 additions and 21 deletions

View File

@ -15,8 +15,8 @@ package org.dromara.hutool.core.text;
import org.dromara.hutool.core.array.ArrayUtil; import org.dromara.hutool.core.array.ArrayUtil;
import org.dromara.hutool.core.collection.CollUtil; import org.dromara.hutool.core.collection.CollUtil;
import org.dromara.hutool.core.comparator.VersionComparator; import org.dromara.hutool.core.comparator.VersionComparator;
import org.dromara.hutool.core.lang.Assert;
import org.dromara.hutool.core.func.SerFunction; import org.dromara.hutool.core.func.SerFunction;
import org.dromara.hutool.core.lang.Assert;
import org.dromara.hutool.core.math.NumberUtil; import org.dromara.hutool.core.math.NumberUtil;
import org.dromara.hutool.core.regex.ReUtil; import org.dromara.hutool.core.regex.ReUtil;
import org.dromara.hutool.core.text.finder.CharFinder; import org.dromara.hutool.core.text.finder.CharFinder;
@ -3304,6 +3304,72 @@ public class CharSequenceUtil extends StrValidator {
return preString + upperFirst(str); return preString + upperFirst(str);
} }
/**
* 方法注释: <br>
* 大写对应下标字母
*
* <pre>例如: str = name,index = 1, return nAme</pre>
*
* <p>搜索关键字:</p>
* <tag>大写</tag>
* <tag>大写下标</tag>
* <tag>大写字母</tag>
* <tag>大小写</tag>
*
* @param str 字符串
* @param index 下标
* @return java.lang.String 字符串
*/
public static String upperIndex(final CharSequence str, final int index) {
if (str == null) {
return null;
}
if (str.length() > 0) {
final char charredAt = str.charAt(index);
if (Character.isLowerCase(charredAt)) {
return subPre(str, index) + Character.toUpperCase(charredAt) + subSuf(str, index + 1);
}
}
return str.toString();
}
/**
* 方法注释: <br>
* 小写对应下标字母
*
* <pre>例如: str = NAME,index = 1, return NaME</pre>
*
* <p>搜索关键字:</p>
* <tag>小写</tag>
* <tag>小写下标</tag>
* <tag>小写字母</tag>
* <tag>大小写</tag>
*
* @param str 字符串
* @param index 下标
* @return java.lang.String 字符串
*/
public static String lowerIndex(final CharSequence str, final int index) {
if (str == null) {
return null;
}
if (str.length() > 0) {
final char charredAt = str.charAt(index);
if (Character.isUpperCase(charredAt)) {
return subPre(str, index) + Character.toLowerCase(charredAt) + subSuf(str, index + 1);
}
}
return str.toString();
}
/** /**
* 大写首字母<br> * 大写首字母<br>
* 例如str = name, return Name * 例如str = name, return Name
@ -3312,16 +3378,7 @@ public class CharSequenceUtil extends StrValidator {
* @return 字符串 * @return 字符串
*/ */
public static String upperFirst(final CharSequence str) { public static String upperFirst(final CharSequence str) {
if (null == str) { return upperIndex(str, 0);
return null;
}
if (str.length() > 0) {
final char firstChar = str.charAt(0);
if (Character.isLowerCase(firstChar)) {
return Character.toUpperCase(firstChar) + subSuf(str, 1);
}
}
return str.toString();
} }
/** /**
@ -3332,16 +3389,7 @@ public class CharSequenceUtil extends StrValidator {
* @return 字符串 * @return 字符串
*/ */
public static String lowerFirst(final CharSequence str) { public static String lowerFirst(final CharSequence str) {
if (null == str) { return lowerIndex(str, 0);
return null;
}
if (str.length() > 0) {
final char firstChar = str.charAt(0);
if (Character.isUpperCase(firstChar)) {
return Character.toLowerCase(firstChar) + subSuf(str, 1);
}
}
return str.toString();
} }
// endregion // endregion

View File

@ -335,4 +335,34 @@ public class CharSequenceUtilTest {
Assertions.assertEquals("This is En", ret); Assertions.assertEquals("This is En", ret);
} }
@Test
public void upperIndexTest() {
final StringBuilder sb = new StringBuilder("key");
final String s1 = CharSequenceUtil.upperIndex(sb, 0);
Assertions.assertEquals("Key", s1);
final String s2 = CharSequenceUtil.upperIndex(sb, 1);
Assertions.assertEquals("kEy", s2);
final String s3 = CharSequenceUtil.upperIndex(sb, 2);
Assertions.assertEquals("keY", s3);
}
@Test
public void lowerIndexTest() {
final StringBuilder sb = new StringBuilder("KEY");
final String s1 = CharSequenceUtil.lowerIndex(sb, 0);
Assertions.assertEquals("kEY", s1);
final String s2 = CharSequenceUtil.lowerIndex(sb, 1);
Assertions.assertEquals("KeY", s2);
final String s3 = CharSequenceUtil.lowerIndex(sb, 2);
Assertions.assertEquals("KEy", s3);
}
} }