Convert string to string[,] in c#
I have a string with data as follows. how do i assign it to a string[,]?
The string when passed has a value like below:
string test = "{ \"BusinessOrg\", \"BusinessOrgID\", \"BusinessOrg\", \"BusinessOrgID\"}, { \"BusinessParameters\", \"BusinessOrgID\", \"BusinessOrg\", \"BusinessOrgID\"}"
The same when viewed in the text vis开发者_如何学运维ualiser is as follows:
{ "BusinessOrg", "BusinessOrgID", "BusinessOrg", "BusinessOrgID"}, { "BusinessParameters", "BusinessOrgID", "BusinessOrg", "BusinessOrgID"} "BusinessOrg", "BusinessOrgID", "BusinessOrg", "BusinessOrgID"}, { "BusinessParameters", "BusinessOrgID", "BusinessOrg", "BusinessOrgID"}
I am unable to assign string to string[,]
string [,] strArray = test;
this is throwing error cannot convert string to string[,]
You will need to build your own parser to deal with this data format you have. Assuming the format of this string is always going to be { "A1", "A2", ... }, { "B1", "B2", ... } ...
and there are no escaped characters (that is, you don't expect a }
or "
within your substrings) then the code is actually fairly straightforward.
As a starting point, you might use this algorithm description. It is not optimized and a bit lax in how it parses (e.g. allowing 0 or more commas/whitespace characters) -- it's just a starting point.
- (A) Eat whitespace and commas until the first
{
is seen.- Allocate a new
string[]
. - (B) Eat whitespace and commas until the first
"
or}
is seen.- If
"
is seen, read characters into a buffer until"
is seen again; store this buffer as a string in the current array, resizing if necessary. Loop again from step (B). - If
}
is seen, and you have not allocated the multi-dimensional array yet, do so now using the length of this array as the second dimension; otherwise, just store it in the array, resizing if necessary. Loop again from step (A).
- If
- Allocate a new
If at some point, you reach the end of the input, you should exit the loop and return your array.
If you're providing that string in code, you need to reformat it to be a 2-d string (e.g. new string[,] { { "a", "b" }, {"c", "d"} }). If you're getting that string from some other source, you need to parse it first. You're taking a string and telling it to convert it to a 2-d string array, which is not possible, since you're only providing a single string.
This may help with the first step (assuming the string is formatted consistently):
String Input = "{ \"BusinessOrg\", \"BusinessOrgID\", \"BusinessOrg\", \"BusinessOrgID\"}, { \"BusinessParameters\", \"BusinessOrgID\", \"BusinessOrg\", \"BusinessOrgID\"}";
String[] Segments = Input.Split(new string[] {"{", "}, {", "}"}, StringSplitOptions.RemoveEmptyEntries);
Then, you can split each segment by comma-space.
I've wrote crazzy but workable solution. Let's compiler do it work... :)
using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Net;
using Microsoft.CSharp;
using System.CodeDom.Compiler;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
string test = "{{ \"BusinessOrg\", \"BusinessOrgID\", \"BusinessOrg\", \"BusinessOrgID\"}, { \"BusinessParameters\", \"BusinessOrgID\", \"BusinessOrg\", \"BusinessOrgID\"}}";
string [,] a = GetArray(test);
string test2 = "{{ \"BusinessOrg2\", \"BusinessOrgID2\", \"BusinessOrg2\", \"BusinessOrgID2\"}, { \"BusinessParameters\", \"BusinessOrgID\", \"BusinessOrg\", \"BusinessOrgID\"}}";
string[,] b = GetArray(test2);
}
static string[,] GetArray(string source)
{
if (source == null)
throw new ArgumentNullException();
string sourceCode =
@"namespace Sample
{
public class ArrayConverter
{
public string [,] GetArray()
{
string [,] s = "+source + ";" +
@" return s;
}
}
}";
Dictionary<string, string> providerOptions = new Dictionary<string, string>
{
{"CompilerVersion", "v3.5"}
};
CSharpCodeProvider provider = new CSharpCodeProvider(providerOptions);
CompilerParameters compilerParams = new CompilerParameters
{GenerateInMemory = true,
GenerateExecutable = false};
CompilerResults results = provider.CompileAssemblyFromSource(compilerParams, sourceCode);
if (results.Errors.Count != 0)
throw new Exception("Mission failed!");
object o = results.CompiledAssembly.CreateInstance("Sample.ArrayConverter");
MethodInfo mi = o.GetType().GetMethod("GetArray");
return (string [,])mi.Invoke(o, null);
}
}
}
精彩评论