遍历字符串
统计字符串中元音个数
public static int countVowels(String input) {
return input.replaceAll("[^aeiouAEIOU]", "").length();
}
public static int countVowels(String input) {
return input.length() - input.replaceAll("[aeiouAEIOU]", "").length();
}
统计字符个数
public static int countChars(String str, char targetChar) {
char[] chars = str.toCharArray();
int count = 0;
for (int i = 0; i < chars.length; i++) {
if (chars[i] == targetChar) {
count++;
}
}
return count;
}
public static int countChars2(String str, char c) {
return (int)IntStream.range(0, str.length()).map(str::charAt).filter(a -> a == c).count();
}
--------------------------------------------------------------------------------
public static int countCharsIgnoreCase(String str, char targetChar) {
char[] chars = str.toLowerCase(Locale.ROOT).toCharArray();
char c = Character.toLowerCase(targetChar);
int count = 0;
for (int i = 0; i < chars.length; i++) {
if (chars[i] == c) {
count++;
}
}
return count;
}
public static int countCharsIgnoreCase2(String str, char ch) {
char[] chars = str.toLowerCase(Locale.ROOT).toCharArray();
char c = Character.toLowerCase(ch);
return (int)IntStream.range(0, str.length()).map(i->chars[i]).filter(a -> a == c).count();
}
--------------------------------------------------------------------------------
public static int countChars2(String str) {
return (int)IntStream.range(0, str.length()).map(str::charAt).filter(a -> 'a' <= a && a <= 'z').count();
}
countChars2("asdzAZ") → 4
检查字符串字符全为数字
public static boolean isNumeric(final String input) {
return IntStream.range(0, input.length()).allMatch(i -> Character.isDigit(input.charAt(i)));
}
命名加工
字符串大小写转化
Ascii.toUpperCase("aaaBBaa"));
Ascii.toLowerCase("AAAaaAA"));
"aaaBBaa".toUpperCase(Locale.ROOT));
"AAAbbAA".toLowerCase(Locale.ROOT));
public static String caseConver(String str) {
int lower = str.length() - str.replaceAll("[a-z]", "").length();
int upper = str.length() - str.replaceAll("[A-Z]", "").length();
if (lower > upper) {
return str.toLowerCase(Locale.ROOT);
} else if (lower < upper) {
return str.toUpperCase(Locale.ROOT);
} else {
return str;
}
}
caseConver("ABCdefgh"));
caseConver("ABCDefgh"));
caseConver("ABCDEfgh"));
判断字符串是否为大/小写
# 判断字符串是否为小写
public static boolean isLowerCase(String input) {
return Objects.equals(input, input.toLowerCase());
}
# 判断字符串是否为大写
public static boolean isUpperCase(String input) {
return Objects.equals(input, input.toUpperCase());
}
将字符串首字母大写
public static String capitalize(String input, boolean lowerRest) {
return input.substring(0, 1).toUpperCase() + (lowerRest ? input.substring(1, input.length()).toLowerCase() : input.substring(1, input.length()));
}
将字符串中每个单词的首字母大写
public static String capitalizeEveryWord(final String input) {
return Pattern.compile("\\b(?=\\w)").splitAsStream(input).map(w -> capitalize(w, false)).collect(Collectors.joining());
}
"hello worldD" → Hello WorldD
驼峰→蛇形
public static String camelCase2Snake(String input, String separator) {
return input.replaceAll("([^A-Z])([A-Z])", "$1" + separator + "$2").toLowerCase();
}
camelCase2Snake("helloWorldHaha", "-") → hello-world-haha
驼峰间→插入separator
public static String insertSeparator2CamelCase(String input, String separator) {
return input.replaceAll("([^A-Z])([A-Z])", "$1" + separator + "$2");
}
insertSeparator2CamelCase("helloWorldHaha", "-"); → hello-World-Haha
字符串拆分
将多行字符串拆分为行数组
public static String[] splitLines(String input) {
return input.split("\\r?\\n");
}
特殊类型字符串
回文子串
NC17 最长回文子串
public static boolean isPalindrome(String s) {
if (s == null || s.length() <= 1) {
return true;
}
int strLength = s.length();
for (int i = 0; i < strLength / 2; i++) {
if (s.charAt(i) != s.charAt(strLength - 1 - i))
return false;
}
return true;
}
public static boolean isPalindrome(String input) {
# \W表示非字母数字下划线,[\W_]表示除了字母数字以外的字符
String s = input.toLowerCase().replaceAll("[\\W_]", "");
return Objects.equals(s, new StringBuilder(s).reverse().toString());
}
isPalindrome("abccba"));
isPalindrome("aa"));
isPalindrome("abc"));
isPalindrome("ab"));
isPalindrome("a"));
isPalindrome(""));
isPalindrome(" "));
public static boolean isBro(String str, String target) {
if (str.length() != target.length()) {
return false;
}
if (str.equals(target)) {
return false;
}
char[] chars = str.toCharArray();
Arrays.sort(chars);
char[] targets = target.toCharArray();
Arrays.sort(targets);
for (int i = 0; i < chars.length; i++) {
if (chars[i] != targets[i]) {
return false;
}
}
return true;
}
isBro("abd","abd"));
isBro("",""));
isBro("abd","aswd"));
isBro("abd",""));
isBro("abd","ade"));
isBro("abc","acb"));
字母+偏移量+模来模拟加密算法
public static char tran(char c, int offset) {
int newChar = c + offset;
if (newChar < 'a') {
return 'a';
} else if ('a' <= newChar && newChar <= 'z') {
return (char) newChar;
} else {
return (char) ((newChar - 'a') % 26 + 'a');
}
}
System.out.println((char) ('a' + (97 - 'a') % 26));
System.out.println((char) ('a' + (98 - 'a') % 26));
System.out.println((char) ('a' + (99 - 'a') % 26));
System.out.println((char) ('a' + (100 - 'a') % 26));
System.out.println((char) ('a' + (101 - 'a') % 26));
System.out.println((char) ('a' + (102 - 'a') % 26));
System.out.println((char) ('a' + (103 - 'a') % 26));
System.out.println((char) ('a' + (104 - 'a') % 26));
System.out.println((char) ('a' + (105 - 'a') % 26));
System.out.println((char) ('a' + (106 - 'a') % 26));
System.out.println((char) ('a' + (107 - 'a') % 26));
System.out.println((char) ('a' + (108 - 'a') % 26));
System.out.println((char) ('a' + (109 - 'a') % 26));
System.out.println((char) ('a' + (110 - 'a') % 26));
System.out.println((char) ('a' + (111 - 'a') % 26));
System.out.println((char) ('a' + (112 - 'a') % 26));
System.out.println((char) ('a' + (113 - 'a') % 26));
System.out.println((char) ('a' + (114 - 'a') % 26));
System.out.println((char) ('a' + (115 - 'a') % 26));
System.out.println((char) ('a' + (116 - 'a') % 26));
System.out.println((char) ('a' + (117 - 'a') % 26));
System.out.println((char) ('a' + (118 - 'a') % 26));
System.out.println((char) ('a' + (119 - 'a') % 26));
System.out.println((char) ('a' + (120 - 'a') % 26));
System.out.println((char) ('a' + (121 - 'a') % 26));
System.out.println((char) ('a' + (122 - 'a') % 26));