27
27
import java .util .List ;
28
28
import java .util .UUID ;
29
29
30
+ import static net .servicestack .client .Func .last ;
31
+
30
32
// Generic Utils
31
33
public class Utils {
32
34
@@ -369,6 +371,14 @@ public static String[] splitOnFirst(String strVal, char needle) {
369
371
: new String [] { strVal .substring (0 , pos ), strVal .substring (pos + 1 ) };
370
372
}
371
373
374
+ public static String [] splitOnFirst (String strVal , String needle ) {
375
+ if (strVal == null ) return new String [0 ];
376
+ int pos = strVal .indexOf (needle );
377
+ return pos == -1
378
+ ? new String [] { strVal }
379
+ : new String [] { strVal .substring (0 , pos ), strVal .substring (pos + needle .length ()) };
380
+ }
381
+
372
382
public static String [] splitOnLast (String strVal , char needle ) {
373
383
if (strVal == null ) return new String [0 ];
374
384
int pos = strVal .lastIndexOf (needle );
@@ -377,6 +387,14 @@ public static String[] splitOnLast(String strVal, char needle) {
377
387
: new String [] { strVal .substring (0 , pos ), strVal .substring (pos + 1 ) };
378
388
}
379
389
390
+ public static String [] splitOnLast (String strVal , String needle ) {
391
+ if (strVal == null ) return new String [0 ];
392
+ int pos = strVal .lastIndexOf (needle );
393
+ return pos == -1
394
+ ? new String [] { strVal }
395
+ : new String [] { strVal .substring (0 , pos ), strVal .substring (pos + needle .length ()) };
396
+ }
397
+
380
398
public static String combinePath (String basePath , String withPath ){
381
399
if (basePath == null )
382
400
basePath = "" ;
@@ -564,4 +582,33 @@ public static boolean equals(String s1, String s2){
564
582
return s1 == null ;
565
583
return s1 .equals (s2 );
566
584
}
585
+
586
+ public static String trimStart (String text , char character ) {
587
+ if (text == null || text .length () == 0 ) return "" ;
588
+
589
+ int i = 0 ;
590
+ while (text .charAt (i ) == character ) {
591
+ i ++;
592
+ }
593
+ return text .substring (i ).trim ();
594
+ }
595
+
596
+ public static String trimEnd (String text , char character ) {
597
+ if (text == null || text .length () == 0 ) return "" ;
598
+
599
+ int i = text .length () - 1 ;
600
+ while (text .charAt (i ) == character ){
601
+ if (--i < 0 ) {
602
+ return "" ;
603
+ }
604
+ }
605
+ return text .substring (0 , i + 1 ).trim ();
606
+ }
607
+
608
+ public static String toHumanFriendlyUrl (String url ){
609
+ if (url == null ) return null ;
610
+
611
+ url = trimEnd (last (splitOnFirst (url , "://" )), '/' );
612
+ return url ;
613
+ }
567
614
}
0 commit comments