001/* 002 * Copyright (c) 2023-2026, Agents-Flex (fuhai999@gmail.com). 003 * <p> 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * <p> 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * <p> 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package com.agentsflex.core.util; 017 018public class StringUtil { 019 020 public static boolean noText(String string) { 021 return !hasText(string); 022 } 023 024 public static boolean hasText(String string) { 025 return string != null && !string.isEmpty() && containsText(string); 026 } 027 028 public static boolean hasText(String... strings) { 029 for (String string : strings) { 030 if (!hasText(string)) { 031 return false; 032 } 033 } 034 return true; 035 } 036 037 private static boolean containsText(CharSequence str) { 038 for (int i = 0; i < str.length(); i++) { 039 if (!Character.isWhitespace(str.charAt(i))) { 040 return true; 041 } 042 } 043 return false; 044 } 045 046 public static String getFirstWithText(String... strings) { 047 if (strings == null) { 048 return null; 049 } 050 for (String str : strings) { 051 if (hasText(str)) { 052 return str; 053 } 054 } 055 return null; 056 } 057 058 public static boolean isJsonObject(String jsonString) { 059 if (noText(jsonString)) { 060 return false; 061 } 062 063 jsonString = jsonString.trim(); 064 return jsonString.startsWith("{") && jsonString.endsWith("}"); 065 } 066 067 public static boolean notJsonObject(String jsonString) { 068 return !isJsonObject(jsonString); 069 } 070 071 072 public static boolean isNumeric(String str) { 073 if (str == null || str.isEmpty()) return false; 074 int len = str.length(), i = 0; 075 if (str.charAt(0) == '+' || str.charAt(0) == '-') { 076 if (len == 1) return false; 077 i = 1; 078 } 079 for (; i < len; i++) { 080 char c = str.charAt(i); 081 if (c < '0' || c > '9') return false; 082 } 083 return true; 084 } 085}