Guest User

Untitled

a guest
May 20th, 2018
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.18 KB | None | 0 0
  1.     //The primary delimiter used in CSV files
  2.     static final String DELIM = "~";
  3.  
  4.     //The secondary delimiter, used to separate multi-valued attributes
  5.     static final String DELIM_TWO = "`";
  6.  
  7.     public static final String ESCAPE = "^";
  8.     public static final String ESCAPE_ENCODING = ESCAPE + "car;";
  9.     public static final String DELIM_ONE_ENCODING = ESCAPE + "til;";
  10.     public static final String DELIM_TWO_ENCODING = ESCAPE + "baq;";
  11.  
  12.     /**
  13.      * Encodes strings so that data does not contain any delimiter characters
  14.      *
  15.      * @param s
  16.      * @return
  17.      */
  18.     public static String encodeString(String s) {
  19.         s = val(s);
  20.  
  21.         if (!findDelimCharacters(s)) {
  22.             return s;
  23.         }
  24.         /*
  25.          * The order that the characters are encoded is important.  If you have the string:
  26.          * "a~^b" and you encoded DELIM first, then you would have "a^til;^b" and then the two
  27.          * carets in that string would get encoded which would be incorrect
  28.          */
  29.         return s.replace(ESCAPE, ESCAPE_ENCODING).
  30.                  replace(DELIM_TWO, DELIM_TWO_ENCODING).
  31.                  replace(DELIM, DELIM_ONE_ENCODING);
  32.     }
Add Comment
Please, Sign In to add comment