|
| 1 | +package com.sap.cloud.sdk.cloudplatform.util; |
| 2 | + |
| 3 | +import java.util.Locale; |
| 4 | + |
| 5 | +import javax.annotation.Nonnull; |
| 6 | +import javax.annotation.Nullable; |
| 7 | + |
| 8 | +import com.google.common.annotations.Beta; |
| 9 | + |
| 10 | +/** |
| 11 | + * Internal utility class for common String operations. |
| 12 | + * |
| 13 | + * @since 5.21.0 |
| 14 | + */ |
| 15 | +@Beta |
| 16 | +public class StringUtils |
| 17 | +{ |
| 18 | + /** |
| 19 | + * Checks if the given string starts with the specified prefix, ignoring case. |
| 20 | + * |
| 21 | + * @param str |
| 22 | + * the string to check |
| 23 | + * @param prefix |
| 24 | + * the prefix to look for |
| 25 | + * @return true if the string starts with the prefix, ignoring case; false otherwise |
| 26 | + */ |
| 27 | + public static boolean startsWithIgnoreCase( @Nullable final String str, @Nullable final String prefix ) |
| 28 | + { |
| 29 | + if( str == null || prefix == null ) { |
| 30 | + return false; |
| 31 | + } |
| 32 | + if( str.length() < prefix.length() ) { |
| 33 | + return false; |
| 34 | + } |
| 35 | + return str.substring(0, prefix.length()).equalsIgnoreCase(prefix); |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * Checks if the given string ends with the specified suffix, ignoring case. |
| 40 | + * |
| 41 | + * @param str |
| 42 | + * the string to check |
| 43 | + * @param suffix |
| 44 | + * the suffix to look for |
| 45 | + * @return true if the string ends with the suffix, ignoring case; false otherwise |
| 46 | + */ |
| 47 | + public static boolean endsWithIgnoreCase( @Nullable final String str, @Nullable final String suffix ) |
| 48 | + { |
| 49 | + if( str == null || suffix == null ) { |
| 50 | + return false; |
| 51 | + } |
| 52 | + if( str.length() < suffix.length() ) { |
| 53 | + return false; |
| 54 | + } |
| 55 | + return str.substring(str.length() - suffix.length()).equalsIgnoreCase(suffix); |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Removes the specified prefix from the start of the string, ignoring case. |
| 60 | + * |
| 61 | + * @param s |
| 62 | + * the string to modify |
| 63 | + * @param prefix |
| 64 | + * the prefix to remove |
| 65 | + * @return the modified string with the prefix removed, or the original string if it did not start with the prefix; |
| 66 | + */ |
| 67 | + |
| 68 | + @Nullable |
| 69 | + public static String removeStartIgnoreCase( @Nullable final String s, @Nullable final String prefix ) |
| 70 | + { |
| 71 | + if( startsWithIgnoreCase(s, prefix) ) { |
| 72 | + return s.substring(prefix.length()); |
| 73 | + } |
| 74 | + return s; |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Removes the specified suffix from the end of the string, ignoring case. |
| 79 | + * |
| 80 | + * @param s |
| 81 | + * the string to modify |
| 82 | + * @param prefix |
| 83 | + * the suffix to remove |
| 84 | + * @return the modified string with the suffix removed, or the original string if it did not end with the suffix; |
| 85 | + */ |
| 86 | + @Nonnull |
| 87 | + public static String removeEndIgnoreCase( @Nonnull final String s, @Nonnull final String prefix ) |
| 88 | + { |
| 89 | + if( endsWithIgnoreCase(s, prefix) ) { |
| 90 | + return s.substring(0, s.length() - prefix.length()); |
| 91 | + } |
| 92 | + return s; |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Checks if the given string is blank or empty. |
| 97 | + * |
| 98 | + * @param s |
| 99 | + * the string to check |
| 100 | + * @return true if the string is null, empty, or contains only whitespace characters; false otherwise |
| 101 | + */ |
| 102 | + public static boolean isBlankOrEmpty( @Nullable final String s ) |
| 103 | + { |
| 104 | + return s == null || s.trim().isEmpty(); |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * Checks if the given CharSequence is blank or empty. |
| 109 | + * |
| 110 | + * @param s |
| 111 | + * the CharSequence to check |
| 112 | + * @return true if the CharSequence is null, empty, or contains only whitespace characters; false otherwise |
| 113 | + */ |
| 114 | + public static boolean isBlankOrEmpty( @Nullable final CharSequence s ) |
| 115 | + { |
| 116 | + return s != null && isBlankOrEmpty(s.toString()); |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * Capitalizes the first character of the given string. |
| 121 | + * |
| 122 | + * @param s |
| 123 | + * the string to capitalize |
| 124 | + * @return the string with the first character capitalized, or null if the input is null or empty |
| 125 | + */ |
| 126 | + @Nullable |
| 127 | + public static String capitalize( @Nullable final String s ) |
| 128 | + { |
| 129 | + if( s == null || s.isEmpty() ) { |
| 130 | + return s; |
| 131 | + } |
| 132 | + return s.substring(0, 1).toUpperCase(Locale.ROOT) + s.substring(1); |
| 133 | + } |
| 134 | + |
| 135 | + /** |
| 136 | + * Uncapitalizes the first character of the given string. |
| 137 | + * |
| 138 | + * @param s |
| 139 | + * the string to uncapitalize |
| 140 | + * @return the string with the first character uncapitalized, or null if the input is null or empty |
| 141 | + */ |
| 142 | + @Nonnull |
| 143 | + public static String uncapitalize( @Nonnull final String s ) |
| 144 | + { |
| 145 | + if( s.isEmpty() ) { |
| 146 | + return s; |
| 147 | + } |
| 148 | + return s.substring(0, 1).toLowerCase(Locale.ROOT) + s.substring(1); |
| 149 | + } |
| 150 | + |
| 151 | + /** |
| 152 | + * Trims the given string and returns null if the result is empty. |
| 153 | + * |
| 154 | + * @param s |
| 155 | + * the string to trim |
| 156 | + * @return the trimmed string, or null if the input is null or empty after trimming |
| 157 | + */ |
| 158 | + @Nullable |
| 159 | + public static String trimToNull( @Nullable String s ) |
| 160 | + { |
| 161 | + return s == null ? null : (s = s.trim()).isEmpty() ? null : s; |
| 162 | + } |
| 163 | + |
| 164 | + /** |
| 165 | + * Prepends the specified prefix to the string if it does not already start with that prefix, ignoring case. |
| 166 | + * |
| 167 | + * @param s |
| 168 | + * the string to modify |
| 169 | + * @param prefix |
| 170 | + * the prefix to prepend |
| 171 | + * @return the modified string with the prefix prepended if it was not already present; otherwise, the original |
| 172 | + * string |
| 173 | + */ |
| 174 | + @Nonnull |
| 175 | + public static String prependIfMissing( @Nonnull final String s, @Nonnull final String prefix ) |
| 176 | + { |
| 177 | + return startsWithIgnoreCase(s, prefix) ? s : prefix + s; |
| 178 | + } |
| 179 | + |
| 180 | + /** |
| 181 | + * Returns the substring of the given string before the first occurrence of the specified suffix character. |
| 182 | + * |
| 183 | + * @param string |
| 184 | + * the string to search |
| 185 | + * @param suffix |
| 186 | + * the character to search for |
| 187 | + * @return the substring before the first occurrence of the suffix character, or the original string if the suffix |
| 188 | + * is not found; |
| 189 | + */ |
| 190 | + @Nullable |
| 191 | + public static String substringBefore( @Nullable final String string, final char suffix ) |
| 192 | + { |
| 193 | + if( string == null ) { |
| 194 | + return string; |
| 195 | + } |
| 196 | + final int pos = string.indexOf(suffix); |
| 197 | + return pos >= 0 ? string.substring(0, pos) : string; |
| 198 | + } |
| 199 | +} |
0 commit comments