java 在Java中将数组字符串转换为字符串并返回
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13271503/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Converting array string to string and back in Java
提问by Janek
I have an array String[] in Java, and must first encode/convert it into a String and then further in the code covert it back to the String[] array. The thing is that I can have any character in a string in String[] array so I must be very careful when encoding. And all the information necessary to decode it must be in the final string. I can not return a string and some other information in an extra variable.
我在 Java 中有一个数组 String[],必须首先将其编码/转换为 String,然后在代码中进一步将其转换回 String[] 数组。问题是我可以在 String[] 数组中的字符串中包含任何字符,因此在编码时我必须非常小心。解码它所需的所有信息都必须在最终字符串中。我无法在额外变量中返回字符串和其他一些信息。
My algorithm I have devised so far is to:
到目前为止,我设计的算法是:
Append all the strings next to each other, for example like this: String[] a = {"lala", "exe", "a"} into String b = "lalaexea"
Append at the end of the string the lengths of all the strings from String[], separated from the main text by $ sign and then each length separated by a comma, so:
将所有字符串彼此相邻附加,例如: String[] a = {"lala", "exe", "a"} into String b = "lalaexea"
在字符串的末尾附加来自 String[] 的所有字符串的长度,用 $ 符号与正文分开,然后每个长度用逗号分隔,所以:
b = "lalaexea$4,3,1"
b = "lalaexea$4,3,1"
Then when converting it back, I would first read the lengths from behind and then based on them, the real strings.
然后在将其转换回来时,我会首先从后面读取长度,然后根据它们读取真正的字符串。
But maybe there is an easier way?
但也许有更简单的方法?
Cheers!
干杯!
回答by Francisco Spaeth
If you don't wanna spend so much time with string operations you could use java serialization + commons codecslike this:
如果你不想花太多时间在字符串操作上,你可以像这样使用 java 序列化 + commons 编解码器:
public void stringArrayTest() throws IOException, ClassNotFoundException, DecoderException {
String[] strs = new String[] {"test 1", "test 2", "test 3"};
System.out.println(Arrays.toString(strs));
// serialize
ByteArrayOutputStream out = new ByteArrayOutputStream();
new ObjectOutputStream(out).writeObject(strs);
// your string
String yourString = new String(Hex.encodeHex(out.toByteArray()));
System.out.println(yourString);
// deserialize
ByteArrayInputStream in = new ByteArrayInputStream(Hex.decodeHex(yourString.toCharArray()));
System.out.println(Arrays.toString((String[]) new ObjectInputStream(in).readObject()));
}
This will return the following output:
这将返回以下输出:
[test 1, test 2, test 3]
aced0005757200135b4c6a6176612e6c616e672e537472696e673badd256e7e91d7b47020000787000000003740006746573742031740006746573742032740006746573742033
[test 1, test 2, test 3]
If you are using maven, you can use the following dependency for commons codec:
如果您使用的是 maven,则可以对 commons 编解码器使用以下依赖项:
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.2</version>
</dependency>
As suggested with base64 (two lines change):
正如 base64 所建议的(两行更改):
String yourString = new String(Base64.encodeBase64(out.toByteArray()));
ByteArrayInputStream in = new ByteArrayInputStream(Base64.decodeBase64(yourString.getBytes()));
In case of Base64 the result string is shorter, for the code exposed below:
在 Base64 的情况下,结果字符串较短,对于下面公开的代码:
[test 1, test 2, test 3]
rO0ABXVyABNbTGphdmEubGFuZy5TdHJpbmc7rdJW5+kde0cCAAB4cAAAAAN0AAZ0ZXN0IDF0AAZ0ZXN0IDJ0AAZ0ZXN0IDM=
[test 1, test 2, test 3]
Regarding the times for each approach, I perform 10^5 executions of each method and the result was as follows:
关于每种方法的时间,我对每种方法执行了 10^5 次,结果如下:
- String manipulation: 156 ms
- Hex: 376 ms
- Base64: 379 ms
- 字符串操作:156 毫秒
- 十六进制:376 毫秒
- Base64:379 毫秒
Code used for test:
用于测试的代码:
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.StringTokenizer;
import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.binary.Hex;
public class StringArrayRepresentationTest {
public static void main(String[] args) throws IOException, ClassNotFoundException, DecoderException {
String[] strs = new String[] {"test 1", "test 2", "test 3"};
long t = System.currentTimeMillis();
for (int i =0; i < 100000;i++) {
stringManipulation(strs);
}
System.out.println("String manipulation: " + (System.currentTimeMillis() - t));
t = System.currentTimeMillis();
for (int i =0; i < 100000;i++) {
testHex(strs);
}
System.out.println("Hex: " + (System.currentTimeMillis() - t));
t = System.currentTimeMillis();
for (int i =0; i < 100000;i++) {
testBase64(strs);
}
System.out.println("Base64: " + (System.currentTimeMillis() - t));
}
public static void stringManipulation(String[] strs) {
String result = serialize(strs);
unserialize(result);
}
private static String[] unserialize(String result) {
int sizesSplitPoint = result.toString().lastIndexOf('$');
String sizes = result.substring(sizesSplitPoint+1);
StringTokenizer st = new StringTokenizer(sizes, ";");
String[] resultArray = new String[st.countTokens()];
int i = 0;
int lastPosition = 0;
while (st.hasMoreTokens()) {
String stringLengthStr = st.nextToken();
int stringLength = Integer.parseInt(stringLengthStr);
resultArray[i++] = result.substring(lastPosition, lastPosition + stringLength);
lastPosition += stringLength;
}
return resultArray;
}
private static String serialize(String[] strs) {
StringBuilder sizes = new StringBuilder("$");
StringBuilder result = new StringBuilder();
for (String str : strs) {
if (sizes.length() != 1) {
sizes.append(';');
}
sizes.append(str.length());
result.append(str);
}
result.append(sizes.toString());
return result.toString();
}
public static void testBase64(String[] strs) throws IOException, ClassNotFoundException, DecoderException {
// serialize
ByteArrayOutputStream out = new ByteArrayOutputStream();
new ObjectOutputStream(out).writeObject(strs);
// your string
String yourString = new String(Base64.encodeBase64(out.toByteArray()));
// deserialize
ByteArrayInputStream in = new ByteArrayInputStream(Base64.decodeBase64(yourString.getBytes()));
}
public static void testHex(String[] strs) throws IOException, ClassNotFoundException, DecoderException {
// serialize
ByteArrayOutputStream out = new ByteArrayOutputStream();
new ObjectOutputStream(out).writeObject(strs);
// your string
String yourString = new String(Hex.encodeHex(out.toByteArray()));
// deserialize
ByteArrayInputStream in = new ByteArrayInputStream(Hex.decodeHex(yourString.toCharArray()));
}
}
回答by Prakash Nadar
Use a Json parser like Hymanson to serialize/deserialize other type of objects as well like integer/floats ext to strings and back.
使用像 Hymanson 这样的 Json 解析器来序列化/反序列化其他类型的对象以及整数/浮点数到字符串并返回。
回答by Luiggi Mendoza
I would use the symbol between the words to later use the String#split
method to get the String back. Based in your $
symbol example, it would be
我会在单词之间使用符号,以便稍后使用该String#split
方法取回字符串。根据您的$
符号示例,它将是
public String mergeStrings(String[] ss) {
StringBuilder sb = new StringBuilder();
for(String s : ss) {
sb.append(s);
sb.append('$');
}
return sb.toString();
}
public String[] unmergeStrings(String s) {
return s.split("\$");
}
Note that in this example, I add a double \
before the $
symbol because the String#split
method receives a regular expression as parameter, and the $
symbol is a special character in regex.
请注意,在此示例中,我\
在$
符号前添加了一个双精度值,因为该String#split
方法接收一个正则表达式作为参数,而$
符号是正则表达式中的特殊字符。
public String processData(String[] ss) {
String mergedString = mergeStrings(ss);
//process data...
//a little example...
for(int i = 0; i < mergedString.length(); i++) {
if (mergedString.charAt(i) == '$') {
System.out.println();
} else {
System.out.print(mergedString.charAt(i));
}
}
System.out.println();
//unmerging the data again
String[] oldData = unmergeStrings(mergedString);
}
In order to support any character in your String[]
, it would be better to set not a single character as separator but instead another String
. The methods would turn into this:
为了支持您的任何字符,String[]
最好不要将单个字符设置为分隔符,而是将另一个String
. 这些方法会变成这样:
public static final String STRING_SEPARATOR = "@|$|@";
public static final String STRING_SEPARATOR_REGEX = "@\|\$\|@";
public String mergeStrings(String[] ss) {
StringBuilder sb = new StringBuilder();
for(String s : ss) {
sb.append(s);
sb.append(STRING_SEPARATOR);
}
return sb.toString();
}
public String[] unmergeStrings(String s) {
return s.split(STRING_SEPARATOR_REGEX);
}
回答by dounyy
Just use a known separator (such as @
or #
to append your strings), then use yourString.split(yourSeparator) to get an array from it.
只需使用已知的分隔符(例如@
或#
附加您的字符串),然后使用 yourString.split(yourSeparator) 从中获取数组。