Skip to content

Commit 9ce4ecd

Browse files
committed
Add new splitOnFirst, splitOnLast, trimStart, trimEnd, toHumanFriendlyUrl methods
1 parent 9f488df commit 9ce4ecd

File tree

1 file changed

+47
-0
lines changed
  • src/AndroidClient/client/src/main/java/net/servicestack/client

1 file changed

+47
-0
lines changed

src/AndroidClient/client/src/main/java/net/servicestack/client/Utils.java

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
import java.util.List;
2828
import java.util.UUID;
2929

30+
import static net.servicestack.client.Func.last;
31+
3032
// Generic Utils
3133
public class Utils {
3234

@@ -369,6 +371,14 @@ public static String[] splitOnFirst(String strVal, char needle) {
369371
: new String[] { strVal.substring(0, pos), strVal.substring(pos + 1) };
370372
}
371373

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+
372382
public static String[] splitOnLast(String strVal, char needle) {
373383
if (strVal == null) return new String[0];
374384
int pos = strVal.lastIndexOf(needle);
@@ -377,6 +387,14 @@ public static String[] splitOnLast(String strVal, char needle) {
377387
: new String[] { strVal.substring(0, pos), strVal.substring(pos + 1) };
378388
}
379389

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+
380398
public static String combinePath(String basePath, String withPath){
381399
if (basePath == null)
382400
basePath = "";
@@ -564,4 +582,33 @@ public static boolean equals(String s1, String s2){
564582
return s1 == null;
565583
return s1.equals(s2);
566584
}
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+
}
567614
}

0 commit comments

Comments
 (0)