Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

24
LINES

< > BotCompany Repo | #1002637 - substring - exception-free (safe) version of String.substring

JavaX fragment (include) [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (55L/1K).

1  
static S substring(S s, int x) {
2  
  ret substring(s, x, strL(s));
3  
}
4  
5  
static S substring(S s, int x, int y) {
6  
  if (s == null) return null;
7  
  if (x < 0) x = 0;
8  
  int n = s.length();
9  
  if (y < x) y = x;
10  
  if (y > n) y = n;
11  
  if (x >= y) ret "";
12  
  return s.substring(x, y);
13  
}
14  
15  
ifclass IntRange
16  
static S substring(S s, IntRange r) {
17  
  ret r == null ? null : substring(s, r.start, r.end);
18  
}
19  
endif
20  
21  
// convenience method for quickly dropping a prefix
22  
sS substring(S s, CharSequence l) {
23  
  ret substring(s, lCharSequence(l));
24  
}

Author comment

Began life as a copy of #1001853

download  show line numbers  debug dex  old transpilations   

Travelled to 16 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mowyntqkapby, mqqgnosmbjvj, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt, xrpafgyirdlv

No comments. add comment

Snippet ID: #1002637
Snippet name: substring - exception-free (safe) version of String.substring
Eternal ID of this version: #1002637/14
Text MD5: 4bd5f88c94b2e6c19242cf331151bba1
Transpilation MD5: 46704d4744af4e00e5e182bab6ca5db8
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2021-05-06 16:11:54
Source code size: 542 bytes / 24 lines
Pitched / IR pitched: No / No
Views / Downloads: 619 / 1870
Version history: 13 change(s)
Referenced in: [show references]