litesoft
@ 803
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 |
package org.litesoft.core.typeutils; import org.litesoft.core.annotations.*; import org.litesoft.core.util.*; public class Strings { public static final String[] EMPTY_ARRAY = new String[0]; private static final String SPACES_32 = " "; // 32 count-em! spaces /** * Adjust a String Array so that it is either null (error condition) or has the <code>DesiredLength</code>. * <p/> * Note: The <code>Source</code> (original) array may be return. * * @param pDesiredLength of the resulting array (must NOT be < 0) * @param pSource array * * @return null if the <code>Source</code> is longer then the <code>DesiredLength</code>; otherwise * an Array that is exactly <code>DesiredLength</code> long (padded with nulls). */ public static @Nullable String[] expectArray( int pDesiredLength, @Nullable String[] pSource ) { Integers.assertNonNegative( "DesiredLength", pDesiredLength ); int zCurrentLength = (pSource == null) ? 0 : pSource.length; if ( pDesiredLength < zCurrentLength ) { return null; } if ( pDesiredLength == zCurrentLength ) { return pSource; } String[] rv = new String[pDesiredLength]; if ( zCurrentLength != 0 ) { System.arraycopy( pSource, 0, rv, 0, zCurrentLength ); } return rv; } /** * Return an String Array made up of the end elements of <code>Strings</code>, but who's first element is * the <code>FromIndex</code> element of <code>Strings</code>. * <p/> * Note: It is OK for <code>Strings</code> to be too short (or even null). * Note: The <code>Strings</code> (original) array may be return. * * @param pStrings array * @param pFromIndex is the index into <code>Strings</code> to start the resulting array from (must be >= 0). * * @return null if */ public static @Nullable String[] everythingFrom( @Nullable String[] pStrings, int pFromIndex ) { Integers.assertNonNegative( "FromIndex", pFromIndex ); if ( (pStrings == null) || (pStrings.length <= pFromIndex) ) { return EMPTY_ARRAY; } String[] rv = new String[pStrings.length - pFromIndex]; System.arraycopy( pStrings, pFromIndex, rv, 0, rv.length ); return rv; } public static String spaces( int pSpaces ) { if ( pSpaces < 1 ) { return ""; } if ( pSpaces <= SPACES_32.length() ) { return SPACES_32.substring( 0, pSpaces ); } // Note use of StringBuilder as StringBuilder was added in 1.5 StringBuilder retval = new StringBuilder( pSpaces ); for ( int i = 0; i < pSpaces; i++ ) { retval.append( ' ' ); } return retval.toString(); } public static String dupChars( char pCharToDup, int pDupCount ) { if ( pDupCount < 1 ) { return ""; } StringBuilder sb = new StringBuilder( pDupCount ); while ( pDupCount-- > 0 ) { sb.append( pCharToDup ); } return sb.toString(); } public static String dup( String pToDup, int pDupCount ) { if ( pToDup == null ) { return null; } if ( pDupCount < 1 ) { return ""; } StringBuilder sb = new StringBuilder( pDupCount * pToDup.length() ); while ( pDupCount-- > 0 ) { sb.append( pToDup ); } return sb.toString(); } public static boolean contains( String pString, String pToFind ) { return UtilsCommon.isNotNull( pString ) && isNotNullOrEmpty( pToFind ) && pString.contains( pToFind ); } public static boolean contains( String pString, char pToFind ) { return UtilsCommon.isNotNull( pString ) && (-1 != pString.indexOf( pToFind )); } public static String noEmpty( String pString ) { return noEmpty( pString, null ); } public static String noEmpty( String pString, String pDefault ) { if ( pString != null ) { pString = pString.trim(); if ( pString.length() != 0 ) { return pString; } } return pDefault; } public static boolean isBlank( String pLine ) { if ( pLine != null ) { for ( int at = pLine.length(); --at >= 0; ) { if ( pLine.charAt( at ) != ' ' ) { return false; } } } return true; } public static boolean areEqualIgnoreCase( String a, String b ) { // noinspection StringEquality return (a == b) || ((null != a) && a.equalsIgnoreCase( b )); } /** * Return if <tt>pThis</tt> starts with <tt>pStartsWith</tt> ignoring * the case of the strings * <p/> * * @param pThis The String to check the front of * @param pStartsWith The String that <tt>pThis</tt>'s front must match * * @return <tt>true</tt> if <tt>pThis</tt> starts with <tt>pStartsWith</tt> ignoring * the case */ public static boolean startsWithIgnoreCase( String pThis, String pStartsWith ) { if ( (pThis != null) && (pStartsWith != null) ) { int lenStartsWith = pStartsWith.length(); if ( lenStartsWith <= pThis.length() ) { return pStartsWith.equalsIgnoreCase( pThis.substring( 0, lenStartsWith ) ); } } return false; } public static String deEmpty( String pString, String pDefault ) { pString = noEmpty( pString ); return (pString != null) ? pString : pDefault; } public static String[] noEmpty( String[] pStrings ) { return (pStrings == null || pStrings.length == 0) ? null : pStrings; } public static String[] noEmpties( String[] pStrings ) { if ( pStrings != null ) { for ( int i = pStrings.length; --i >= 0; ) { String zString = noEmpty( pStrings[i] ); if ( zString == null ) { pStrings = UtilsCommon.removeStringFromArray( pStrings, i ); } pStrings[i] = zString; } if ( pStrings.length == 0 ) { pStrings = null; } } return pStrings; } public static String[] deNull( String[] pStrings ) { return (pStrings != null) ? pStrings : EMPTY_ARRAY; } public static String deNull( CharSequence pString ) { return deNull( pString, "" ); } public static String deNull( CharSequence pString, String pDefault ) { return (pString != null) ? pString.toString() : pDefault; } public static boolean isNotNullOrEmpty( String pStringToCheck ) { return ((pStringToCheck != null) && (pStringToCheck.trim().length() != 0)); } public static boolean allEmpty( String[] pParts ) { if ( pParts != null ) { for ( String part : pParts ) { if ( isNotNullOrEmpty( part ) ) { return false; } } } return true; } public static boolean isNullOrEmptyOrSpaces( String pStringToCheck ) { return ((pStringToCheck == null) || // (pStringToCheck.length() == 0) || // (-1 != pStringToCheck.indexOf( ' ' ))); } public static boolean isNullOrEmpty( String pStringToCheck ) { return ((pStringToCheck == null) || (pStringToCheck.trim().length() == 0)); } public static String trimLeadingSpaces( String pStr ) { if ( (pStr == null) || !pStr.startsWith( " " ) ) { return pStr; } int sLen = pStr.length(); for ( int i = 0; i < sLen; i++ ) { if ( pStr.charAt( i ) != ' ' ) { return pStr.substring( i ); } } return ""; } public static String trimTrailingSpaces( String pStr ) { if ( (pStr == null) || !pStr.endsWith( " " ) ) { return pStr; } int sLen = pStr.length(); for ( int i = sLen - 1; --i >= 0; ) { if ( pStr.charAt( i ) != ' ' ) { return pStr.substring( 0, i + 1 ); } } return ""; } public static String[] parseChar( String pStringToParse, char pSeparator ) { if ( isNullOrEmpty( pStringToParse ) ) { return new String[0]; } int count = 1; int from = 0; for ( int at; -1 != (at = pStringToParse.indexOf( pSeparator, from )); from = at + 1 ) { count++; } String[] parts = new String[count]; count = from = 0; for ( int at; -1 != (at = pStringToParse.indexOf( pSeparator, from )); from = at + 1 ) { parts[count++] = pStringToParse.substring( from, at ); } parts[count] = pStringToParse.substring( from ); return parts; } public static String replace( String pSource, char pToFind, String pToReplaceWith ) { if ( (pSource != null) && (pToReplaceWith != null) ) { int removeFor = 1; int adjustFromBy = pToReplaceWith.length(); int from = 0; for ( int at; -1 != (at = pSource.indexOf( pToFind, from )); from = at + adjustFromBy ) { pSource = pSource.substring( 0, at ) + pToReplaceWith + pSource.substring( at + removeFor ); } } return pSource; } public static String replace( String pSource, String pToFind, String pToReplaceWith ) { if ( (pSource != null) && (pToFind != null) && (pToReplaceWith != null) ) { int removeFor = pToFind.length(); int adjustFromBy = pToReplaceWith.length(); int from = 0; for ( int at; -1 != (at = pSource.indexOf( pToFind, from )); from = at + adjustFromBy ) { pSource = pSource.substring( 0, at ) + pToReplaceWith + pSource.substring( at + removeFor ); } } return pSource; } public static String defaultIfNull( String pTestString, String pDefault ) { return isNullOrEmpty( pTestString ) ? pDefault : pTestString; } public static String normalizeNewLines( String pString ) { pString = replace( pString, "\r\n", "\n" ); pString = replace( pString, "\n\r", "\n" ); pString = replace( pString, '\r', "\n" ); return pString; } public static String[] stringToLines( String pString ) { return parseChar( replace( normalizeNewLines( pString ), "\f", "\n" ), '\n' ); } public static String linesToString( String[] pLines ) { StringBuilder sb = new StringBuilder(); if ( pLines != null ) { for ( String zLine : pLines ) { sb.append( zLine ); sb.append( '\n' ); } } return sb.toString(); } public static String makeNonBlankLines( String pSource, int pLinesToMake ) { pSource = normalizeNewLines( deNull( pSource ) ); StringBuilder sb = new StringBuilder(); for ( int i = 0; i < pLinesToMake; i++ ) { int at = (pSource += '\n').indexOf( '\n' ); String line = pSource.substring( 0, at ); pSource = pSource.substring( at + 1 ); if ( sb.length() != 0 ) { sb.append( '\n' ); } sb.append( line ).append( ' ' ); } return sb.toString(); } } |