1 /* 2 * Copyright (c) 2025 Shenzhen Kaihong Digital. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 package utils; 17 18 import java.util.Locale; 19 20 /** 21 * <h3>类名:该类用于xxx</h3> 22 * description string utils class 23 * 24 * @author Administrator 25 * date 2025-02-28 26 * @version 1.0 27 * @since 2025-02-28 28 */ 29 public class StringUtils { 30 /** 31 * split string by char 32 * 33 * @param str string need to split 34 * @param delimiter the split char 35 */ splitByChar(String str, char delimiter)36 public static void splitByChar(String str, char delimiter) { 37 int index = str.indexOf(delimiter); 38 if (index != -1) { 39 String prefix = str.substring(0, index); 40 String suffix = str.substring(index + 1).toLowerCase(Locale.ROOT); 41 System.out.println("前缀: " + prefix); 42 System.out.println("后缀: " + suffix); 43 } else { 44 System.out.println("未找到分隔符 '" + delimiter + "'"); 45 } 46 } 47 48 /** 49 * 删除最后的一个空格 50 * 51 * @param str 删除字符串最后一个空格 52 * @return 返回字符串 53 */ removeLastSpace(String str)54 public static String removeLastSpace(String str) { 55 if (str != null && !str.isEmpty() && str.charAt(str.length() - 1) == ' ') { 56 return str.substring(0, str.length() - 1); 57 } 58 return str; 59 } 60 61 /** 62 * 删除最后的几个字符 63 * 64 * @param str 删除字符串最后一个空格 65 * @param n 需要删除字符的个数 66 * @return 返回字符串 67 */ removeLastCharacter(String str, int n)68 public static String removeLastCharacter(String str, int n) { 69 if (str != null && !str.isEmpty() && str.length() > n) { 70 return str.substring(0, str.length() - n); 71 } 72 return str; 73 } 74 75 /** 76 * 判读是否为数字 77 * 78 * @param str 字符串 79 * @return 是数字返回 true 80 */ isAllDigits(String str)81 public static boolean isAllDigits(String str) { 82 return str != null && str.matches("\\d+"); 83 } 84 85 /** 86 * 判断是否为布尔 87 * 88 * @param str 字符串 89 * @return 是布尔返回true 90 */ isBoolean(String str)91 public static boolean isBoolean(String str) { 92 String lowerStr = str.toLowerCase(Locale.ROOT); 93 return lowerStr.equals("true") || lowerStr.equals("false"); 94 } 95 96 /** 97 * 小写第一个字母 98 * 99 * @param input 字符串 100 * @return 返回字符串 101 */ unCapitalFirst(String input)102 public static String unCapitalFirst(String input) { 103 if (input == null || input.isEmpty()) { 104 return input; 105 } 106 return input.substring(0, 1).toLowerCase(Locale.ROOT) + input.substring(1); 107 } 108 109 /** 110 * 大写第一个字母 111 * 112 * @param input 字符串 113 * @return 返回字符串 114 */ capitalFirst(String input)115 public static String capitalFirst(String input) { 116 if (input == null || input.isEmpty()) { 117 return input; 118 } 119 return input.substring(0, 1).toUpperCase(Locale.ROOT) + input.substring(1); 120 } 121 } 122