简体   繁体   English

将两个字符串编码/解码为一个字符串,然后返回两个

[英]Encode/decode two strings into one string, and back to two

This is an interview question. 这是一个面试问题。 I was thinking of a solution in java. 我在考虑Java中的解决方案。 This questions seems very simple, is there a catch here? 这个问题似乎很简单,这里有一个问题吗?

I was thinking of the following solution: 我在想以下解决方案:

string1 + 1*hash(String1) + string2 + 2*hash(String2).

If I concat strings like this, then I can decode them as well easily into 2 separate strings. 如果我像这样连接字符串,那么我也可以轻松地将它们解码为2个单独的字符串。

Am I missing something in the question? 我在问题中遗漏了什么吗?

Encode: 编码:

String encoded = new JsonArray().add(str1).add(str2).toString();

Decode: 解码:

JsonArray arr = JsonArray.readFrom(encoded);
String str1 = arr.get(0).asString();
String str2 = arr.get(1).asString();

Here I use minimal-json lib, but it's pretty similar with any other JSON library as well. 在这里,我使用了minimal-json库,但是它与其他任何JSON库也非常相似。

Note that it's usually a bad idea to invent new formats of encoding the information into the string as you have plenty of existing ones (xml, json, yaml, etc.) which already solved all the possible issues like symbol escaping and exception handling. 请注意,由于已经有很多现有的格式(xml,json,yaml等)已经解决了所有可能的问题,例如符号转义和异常处理,因此发明一种将信息编码为字符串的新格式通常是一个坏主意。

To encode: 编码:

String encoded = ""+str1.length()+"/"+str1+str2;

To decode: 解码:

String[] temp = encoded.split("/", 2);
int length1 = Integer.parseInt(temp[0]);
String str1 = temp[1].substring(0, length1);
String str2 = temp[1].substring(length1);

Explanation: 说明:
The encoded string is in the form "<number>/<str1><str2>" . 编码的字符串格式为"<number>/<str1><str2>" When you call split(regex, limit) the size of the resulting array will be at most limit , considering only the first matches of regex . 当您调用split(regex, limit) ,仅考虑regex的第一个匹配项,结果数组的大小将为最大limit Thus even if your strings contain the character / you can be sure that the resulting array will be {"<number>", "<str1><str2>"} . 因此,即使您的字符串包含字符/ ,也可以确保结果数组将为{"<number>", "<str1><str2>"}
the substring(begin, end) return a string starting at begin inclusive and ending at end exclusive , giving you a resulting substring of end-begin length. substring(begin, end)返回一个字符串substring(begin, end)该字符串从begin inclusiveend Exclusive end ,从而为您提供一个end-begin长度的子字符串。 Since you are calling it with values (0, str1.length()) what you get is exactly str1 . 由于您使用值(0, str1.length())调用它(0, str1.length())得到的正是str1 The last call will return a substring from str1.length() , which is also the index of the first character of str2 , to the end of the string (which is the end of str2 ). 最后一次调用将从str1.length()返回子字符串,该子字符串也是str2的第一个字符的索引,到字符串的末尾(也就是str2的末尾)。
Reference: String javadoc page 参考: String javadoc页面

One way is to use the length of the first string. 一种方法是使用第一个字符串的长度。

// encode
String concat = string1 + string2;

// decode
String str1 = concat.substring( 0, string1.length() );
String str2 = concat.substring( string1.length(), concat.length() );

Another way is to use a delimiter. 另一种方法是使用定界符。 But the delimiter character should not be included in any of the strings to be joined. 但是分隔符不应包含在要连接的任何字符串中。

// encode
String concat = "hello" + "`" + "world!";

// decode
String[] decoded = concat.split("`");
String str1 = decoded[0];
String str2 = decoded[1];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM