java - Is there a way to concatenate strings with a delimiter that a user will never be able to enter across languages and keyboard types? -
i have 5 strings want concatenate storage. can not store them separately, can not serialize object contains them. can store 1 string variable.
is there way add these strings together, separated character programmer enter said string can split using character later? there unique escape code or of sort?
this may bad design, , changing system completely, curious if possible.
if can not control input, can't guarantee anything.
it's simple.
instead, should pick delimiter, escape delimiter.
consider, ubiquitous tab.
to escape tabs, using \ escape character:
field1 = field1.replace("\\", "\\\\").replace("\t", "\\\t");
then have parse line back, straight forward.
here's complete example. use string builders , more interesting println fields.
public class x { public static void main(string args[]) { string field1 = "field\t1"; string field2 = "field\\2"; string field3 = "field3"; field1 = field1.replace("\\", "\\\\").replace("\t", "\\\t"); field2 = field2.replace("\\", "\\\\").replace("\t", "\\\t"); field3 = field3.replace("\\", "\\\\").replace("\t", "\\\t"); string line = field1 + "\t" + field2 + "\t" + field3; system.out.println("encoded line: " + line); char chars[] = line.tochararray(); string field = ""; boolean escaped = false; for(char c : chars) { if (escaped) { field = field + c; escaped = false; } else { switch (c) { case '\\': escaped = true; break; case '\t': system.out.println("field = " + field); field = ""; break; default: field = field + c; break; } } } if (!field.isempty()) { system.out.println("field = " + field); } } }
addenda:
i don't know xml has anything, there's no mention of xml in question.
you mentioned in comment:
playlists universalized in android , playlists not created in app not able stick rules.
that means don't control input, since "will not able stick rules". so, have no idea they'll send. have no idea how may using character "can never used". ideally isn't security issue, these classic examples of how data gets injected don't want injected.
so, means need more precise on rules rather "hoping" send formatted data. using actual syntax accept "anything", not problem @ all.
you can use simple tab example, works character, really, it's escaping makes work, actual character used irrelevant.
or can use more sophisticated syntax. json mentioned, it's ubiquitous.
either 1 precise, , can take field values of "anything", imposing no limitation on users can or can't type. knows magic keyboards coming next. welcome world of emojis.
finally, if we've learned in recent past, assuming data coming in socket matching spec , not need validated plain dangerous. may not program talking port, anything.
more addenda:
i referring xml because when xml file written , java, how determine xml tag, , in between tag?
because xml document represented internal data structure (for example, standard dom). using dom example, dom know "what xml tag, , in between tag", , when serializes dom xml, escapes special characters used xml using things '<' '<' character when appear "between tags".
when parsing xml, parser knows it's parsing element tag or not, knows whether data parsing between tag or not. xmls grammar describes rules this.
Comments
Post a Comment