mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
add lowerAt and upperAt
This commit is contained in:
parent
d5196ff687
commit
42e3c66af9
@ -3305,65 +3305,60 @@ public class CharSequenceUtil extends StrValidator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 方法注释: <br>
|
* 大写对应下标字母
|
||||||
* 〈大写对应下标字母〉
|
|
||||||
*
|
*
|
||||||
* <pre>例如: str = name,index = 1, return nAme</pre>
|
* <pre>例如: str = name,index = 1, return nAme</pre>
|
||||||
*
|
*
|
||||||
* <p>搜索关键字:</p>
|
|
||||||
* <tag>大写</tag>
|
|
||||||
* <tag>大写下标</tag>
|
|
||||||
* <tag>大写字母</tag>
|
|
||||||
* <tag>大小写</tag>
|
|
||||||
*
|
|
||||||
* @param str 字符串
|
* @param str 字符串
|
||||||
* @param index 下标
|
* @param index 下标,支持负数,-1表示最后一个字符
|
||||||
* @return java.lang.String 字符串
|
* @return 字符串
|
||||||
*/
|
*/
|
||||||
public static String upperIndex(final CharSequence str, final int index) {
|
public static String upperAt(final CharSequence str, int index) {
|
||||||
|
if (null == str) {
|
||||||
if (str == null) {
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (str.length() > 0) {
|
// 支持负数
|
||||||
final char charredAt = str.charAt(index);
|
final int length = str.length();
|
||||||
if (Character.isLowerCase(charredAt)) {
|
if (index < 0) {
|
||||||
return subPre(str, index) + Character.toUpperCase(charredAt) + subSuf(str, index + 1);
|
index += length;
|
||||||
}
|
}
|
||||||
}
|
if (index < 0 || index >= length) {
|
||||||
|
|
||||||
return str.toString();
|
return str.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
final char c = str.charAt(index);
|
||||||
|
if (Character.isLowerCase(c)) {
|
||||||
|
return subPre(str, index) + Character.toUpperCase(c) + subSuf(str, index + 1);
|
||||||
|
}
|
||||||
|
return str.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 方法注释: <br>
|
* 小写对应下标字母<br>
|
||||||
* 〈小写对应下标字母〉
|
* 例如: str = NAME,index = 1, return NaME
|
||||||
*
|
|
||||||
* <pre>例如: str = NAME,index = 1, return NaME</pre>
|
|
||||||
*
|
|
||||||
* <p>搜索关键字:</p>
|
|
||||||
* <tag>小写</tag>
|
|
||||||
* <tag>小写下标</tag>
|
|
||||||
* <tag>小写字母</tag>
|
|
||||||
* <tag>大小写</tag>
|
|
||||||
*
|
*
|
||||||
* @param str 字符串
|
* @param str 字符串
|
||||||
* @param index 下标
|
* @param index 下标,支持负数,-1表示最后一个字符
|
||||||
* @return java.lang.String 字符串
|
* @return 字符串
|
||||||
*/
|
*/
|
||||||
public static String lowerIndex(final CharSequence str, final int index) {
|
public static String lowerAt(final CharSequence str, int index) {
|
||||||
|
|
||||||
if (str == null) {
|
if (str == null) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (str.length() > 0) {
|
// 支持负数
|
||||||
final char charredAt = str.charAt(index);
|
final int length = str.length();
|
||||||
if (Character.isUpperCase(charredAt)) {
|
if (index < 0) {
|
||||||
return subPre(str, index) + Character.toLowerCase(charredAt) + subSuf(str, index + 1);
|
index += length;
|
||||||
}
|
}
|
||||||
|
if (index < 0 || index >= length) {
|
||||||
|
return str.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
final char c = str.charAt(index);
|
||||||
|
if (Character.isUpperCase(c)) {
|
||||||
|
return subPre(str, index) + Character.toLowerCase(c) + subSuf(str, index + 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
return str.toString();
|
return str.toString();
|
||||||
@ -3378,7 +3373,7 @@ public class CharSequenceUtil extends StrValidator {
|
|||||||
* @return 字符串
|
* @return 字符串
|
||||||
*/
|
*/
|
||||||
public static String upperFirst(final CharSequence str) {
|
public static String upperFirst(final CharSequence str) {
|
||||||
return upperIndex(str, 0);
|
return upperAt(str, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -3389,7 +3384,7 @@ public class CharSequenceUtil extends StrValidator {
|
|||||||
* @return 字符串
|
* @return 字符串
|
||||||
*/
|
*/
|
||||||
public static String lowerFirst(final CharSequence str) {
|
public static String lowerFirst(final CharSequence str) {
|
||||||
return lowerIndex(str, 0);
|
return lowerAt(str, 0);
|
||||||
}
|
}
|
||||||
// endregion
|
// endregion
|
||||||
|
|
||||||
|
@ -117,6 +117,12 @@ public class CharSequenceUtilTest {
|
|||||||
Assertions.assertEquals(41, v.getBytes(CharsetUtil.GBK).length);
|
Assertions.assertEquals(41, v.getBytes(CharsetUtil.GBK).length);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void subPreTest() {
|
||||||
|
final String pre = CharSequenceUtil.subPre("abc", 0);
|
||||||
|
Assertions.assertEquals(StrUtil.EMPTY, pre);
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void startWithTest() {
|
public void startWithTest() {
|
||||||
// https://gitee.com/dromara/hutool/issues/I4MV7Q
|
// https://gitee.com/dromara/hutool/issues/I4MV7Q
|
||||||
@ -337,32 +343,31 @@ public class CharSequenceUtilTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void upperIndexTest() {
|
void upperAtTest() {
|
||||||
|
|
||||||
final StringBuilder sb = new StringBuilder("key");
|
final StringBuilder sb = new StringBuilder("key");
|
||||||
|
|
||||||
final String s1 = CharSequenceUtil.upperIndex(sb, 0);
|
final String s1 = CharSequenceUtil.upperAt(sb, 0);
|
||||||
Assertions.assertEquals("Key", s1);
|
Assertions.assertEquals("Key", s1);
|
||||||
|
|
||||||
final String s2 = CharSequenceUtil.upperIndex(sb, 1);
|
final String s2 = CharSequenceUtil.upperAt(sb, 1);
|
||||||
Assertions.assertEquals("kEy", s2);
|
Assertions.assertEquals("kEy", s2);
|
||||||
|
|
||||||
final String s3 = CharSequenceUtil.upperIndex(sb, 2);
|
final String s3 = CharSequenceUtil.upperAt(sb, 2);
|
||||||
Assertions.assertEquals("keY", s3);
|
Assertions.assertEquals("keY", s3);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void lowerIndexTest() {
|
void lowerAtTest() {
|
||||||
final StringBuilder sb = new StringBuilder("KEY");
|
final StringBuilder sb = new StringBuilder("KEY");
|
||||||
|
|
||||||
final String s1 = CharSequenceUtil.lowerIndex(sb, 0);
|
final String s1 = CharSequenceUtil.lowerAt(sb, 0);
|
||||||
Assertions.assertEquals("kEY", s1);
|
Assertions.assertEquals("kEY", s1);
|
||||||
|
|
||||||
final String s2 = CharSequenceUtil.lowerIndex(sb, 1);
|
final String s2 = CharSequenceUtil.lowerAt(sb, 1);
|
||||||
Assertions.assertEquals("KeY", s2);
|
Assertions.assertEquals("KeY", s2);
|
||||||
|
|
||||||
final String s3 = CharSequenceUtil.lowerIndex(sb, 2);
|
final String s3 = CharSequenceUtil.lowerAt(sb, 2);
|
||||||
Assertions.assertEquals("KEy", s3);
|
Assertions.assertEquals("KEy", s3);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user