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

15
LINES

< > BotCompany Repo | #1000644 - shorten function

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

Libraryless. Click here for Pure Java version (2540L/16K).

static int shorten_default = 100;

sS shorten(CharSequence s) { ret shorten(s, shorten_default); }

sS shorten(CharSequence s, int max) {
  ret shorten(s, max, "...");
}

sS shorten(CharSequence s, int max, S shortener) {
  if (s == null) ret "";
  if (max < 0) ret str(s);
  return s.length() <= max ? str(s) : subCharSequence(s, 0, min(s.length(), max-l(shortener))) + shortener;
}

sS shorten(int max, CharSequence s) { ret shorten(s, max); }

download  show line numbers  debug dex   

Travelled to 14 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, ddnzoavkxhuk, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, onxytkatvevr, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, xrpafgyirdlv

No comments. add comment

Snippet ID: #1000644
Snippet name: shorten function
Eternal ID of this version: #1000644/9
Text MD5: 05874cd24b584b697eeadfc8814cfca6
Transpilation MD5: f82e05803c4a06693700102b6a704571
Author: stefan
Category:
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2020-07-25 12:47:08
Source code size: 459 bytes / 15 lines
Pitched / IR pitched: No / No
Views / Downloads: 509 / 9197
Version history: 8 change(s)
Referenced in: [show references]

Formerly at http://tinybrain.de/1000644 & http://1000644.tinybrain.de