将数组字符串转换为字符串并返回Java [英] Converting array string to string and back in Java
问题描述
我迄今为止设计的算法是:
-
将所有字符串相互附加,例如:
String [] a = {lala,exe,a }
into
String b =lalaexea -
在字符串末尾附加所有字符串的长度,从String [ ],与主文本分开$号,然后每个长度用逗号分隔,所以:
b = lalaexea $ 4,3,1
然后转换回来,我会先从后面读取长度,然后根据它们,真正的字符串。
可能还有一个更简单的方法?
干杯!
如果您不想花太多时间使用字符串操作,可以使用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);
//你的字符串
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()));
}
这将返回以下输出:
[test 1,test 2,test 3]
aced0005757200135b4c6a6176612e6c616e672e537472696e673badd256e7e91d7b47020000787000000003740006746573742031740006746573742032740006746573742033
[测试1,测试2,测试3]
如果您使用maven,可以使用以下依赖关系来使用commons codec:
<依赖关系>
< groupId> commons-codec< / groupId>
< artifactId> commons-codec< / artifactId>
< version> 1.2< / version>
< / dependency>
如base64(两行更改)所示:
String yourString = new String(Base64.encodeBase64(out.toByteArray()));
ByteArrayInputStream in = new ByteArrayInputStream(Base64.decodeBase64(yourString.getBytes()));
如果是Base64,结果字符串较短,下面显示的代码如下:
[测试1,测试2,测试3]
rO0ABXVyABNbTGphdmEubGFuZy5TdHJpbmc7rdJW5 + kde0cCAAB4cAAAAAN0AAZ0ZXN0IDF0AAZ0ZXN0IDJ0AAZ0ZXN0IDM =
[测试1,测试2,测试3 ]
对于每种方法的时间,我执行每个方法的10 ^ 5执行,结果是如下:
- 字符串操作:156 ms
- 十六进制:376 ms
- Base64:379 ms
用于测试的代码:
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(); (int i = 0; i <100000; i ++)
{
stringManipulation(strs);
}
System.out.println(String manipulation:+(System.currentTimeMillis() - t));
t = System.currentTimeMillis(); (int i = 0; i <100000; i ++)
{
testHex(strs);
}
System.out.println(Hex:+(System.currentTimeMillis() - t));
t = System.currentTimeMillis(); (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('$');
字符串大小= 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);
//你的字符串
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);
//你的字符串
String yourString = new String(Hex.encodeHex(out.toByteArray()));
// deserialize
ByteArrayInputStream in = new ByteArrayInputStream(Hex.decodeHex(yourString.toCharArray()));
}
}
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.
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:
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!
If you don't wanna spend so much time with string operations you could use java serialization + commons codecs like this:
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:
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.2</version>
</dependency>
As suggested with base64 (two lines change):
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:
[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:
- String manipulation: 156 ms
- Hex: 376 ms
- Base64: 379 ms
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()));
}
}
这篇关于将数组字符串转换为字符串并返回Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!