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

30
LINES

< > BotCompany Repo | #1005595 - quote - Java-compatible string quoting with ""

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

Libraryless. Click here for Pure Java version (705L/5K/19K).

static S quote(O o) {
  if (o == null) return "null";
  ret quote(str(o));
}

static S quote(S s) {
  if (s == null) return "null";
  StringBuilder out = new StringBuilder((int) (l(s)*1.5+2));
  quote_impl(s, out);
  ret out.toString();
}
  
static void quote_impl(S s, StringBuilder out) {
  out.append('"');
  int l = s.length();
  for (int i = 0; i < l; i++) {
    char c = s.charAt(i);
    if (c == '\\' || c == '"')
      out.append('\\').append(c);
    else if (c == '\r')
      out.append("\\r");
    else if (c == '\n')
      out.append("\\n");
    else if (c == '\0')
      out.append("\\0");
    else
      out.append(c);
  }
  out.append('"');
}

Author comment

Began life as a copy of #1001239

download  show line numbers  debug dex   

Travelled to 11 computer(s): aoiabmzegqzx, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, podlckwnjdmb, tslmcundralx, tvejysmllsmz, whxojlpjdney

No comments. add comment

Snippet ID: #1005595
Snippet name: quote - Java-compatible string quoting with ""
Eternal ID of this version: #1005595/4
Text MD5: ee0f7e61fcaecc9c7bdc5ac492216526
Transpilation MD5: c93397376fa83091e25c1c6d009a9b72
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2019-01-18 04:28:14
Source code size: 685 bytes / 30 lines
Pitched / IR pitched: No / No
Views / Downloads: 420 / 780
Version history: 3 change(s)
Referenced in: [show references]

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