转换多维数组到字符串,返回 [英] Convert Multi-Dimensional Array to String and Back

查看:168
本文介绍了转换多维数组到字符串,返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个二维数组,我需要能够转换为字符串重新presentation和回阵列格式。我想创建一个generci方法将处理任何数组1D,2D,3D等,这样我就可以重新使用在未来的方法。

什么是会对此最好的方法是什么?

 字符串[,] _array =新的字符串[_helpTextItemNames.Count,2]。
 

解决方案

如果你不关心太多关于字符串的结构,那么<一个href="http://msdn.microsoft.com/en-us/library/system.runtime.serialization.formatters.soap.soapformatter.aspx"相对=nofollow> SoapFormatter 是一个选项。这里是一个快速和肮脏的例子。没有pretty的,但它可能为你工作。

 公共静态类助手
{
  公共静态字符串的ObjectToString(阵列AR)
  {
    使用(MemoryStream的毫秒=新的MemoryStream())
    {
      SoapFormatter格式化=新SoapFormatter();
      formatter.Serialize(MS,AR);
      返回Encoding.UTF8.GetString(ms.ToArray());
    }
  }

  公共静态对象ObjectFromString(字符串s)
  {
    使用(MemoryStream的毫秒=新的MemoryStream(Encoding.UTF8.GetBytes(S)))
    {
      SoapFormatter格式化=新SoapFormatter();
      返回formatter.Deserialize(毫秒),为阵列;
    }
  }

  公共静态牛逼ObjectFromString&LT; T&GT;(字符串s)
  {
    返程(T)Helpers.ObjectFromString(S);
  }
}
 

这些助手可用于任意变换序列化对象到一个字符串,包括阵列,只要数组的元素是可序列化

  //序列化一个一维数组,字符串格式
  烧焦[]芳= {'1','2','3'};
  Console.WriteLine(Helpers.ObjectToString(AR));

  //序列化一个二维数组到一个字符串格式
  炭[,] AR2 = {{'1','2','3'},{'一','B','C'}};
  Console.WriteLine(Helpers.ObjectToString(AR2));

  //反序列化从字符串格式的数组
  的char [,] AR3 = Helpers.ObjectFromString(Helpers.ObjectToString(AR2))为char [,]
  的char [,] AR4 = Helpers.ObjectFromString&LT;的char [,]&GT;(Helpers.ObjectToString(AR2));
 

I have a 2D array that I need to be able to convert to a string representation and back to array format. I would like to create a generci method that will handle any array 1d, 2d, 3d etc. so I can reuse the method in future.

What is the best way of going about this?

string[,] _array = new string[_helpTextItemNames.Count, 2];

解决方案

If you do not care to much about the structure of the string then the SoapFormatter is an option. Here is a quick and dirty example. Not pretty but it might work for you.

public static class Helpers
{    
  public static string ObjectToString(Array ar)
  {      
    using (MemoryStream ms = new MemoryStream())
    {
      SoapFormatter formatter = new SoapFormatter();
      formatter.Serialize(ms, ar);
      return Encoding.UTF8.GetString(ms.ToArray());
    }
  }

  public static object ObjectFromString(string s)
  {
    using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(s)))
    {
      SoapFormatter formatter = new SoapFormatter();
      return formatter.Deserialize(ms) as Array;
    }
  }

  public static T ObjectFromString<T>(string s)
  {
    return (T)Helpers.ObjectFromString(s);
  }
}

These helpers can be used to transform any Serializable object to a string, including arrays, as long as the elements of the array are serializable.

  // Serialize a 1 dimensional array to a string format
  char[] ar = { '1', '2', '3' };
  Console.WriteLine(Helpers.ObjectToString(ar));

  // Serialize a 2 dimensional array to a string format
  char[,] ar2 = {{ '1', '2', '3' },{ 'a', 'b', 'c' }};
  Console.WriteLine(Helpers.ObjectToString(ar2));

  // Deserialize an array from the string format
  char[,] ar3 = Helpers.ObjectFromString(Helpers.ObjectToString(ar2)) as char[,];
  char[,] ar4 = Helpers.ObjectFromString<char[,]>(Helpers.ObjectToString(ar2));

这篇关于转换多维数组到字符串,返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆