• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 6214
  • Last Modified:

How to Split a string with multiple delimiters into a multidimensional array in C#

I have a string with multiple delimters
     e.g.   string myList = A:1,B:2,C:3,D:4;
And I would like to parse it into a multi-dimensional array like:
    string[,] myArray = myList.Split(new char[] {":",","} );

How can I do this without splitting the string twice or doing some for loop, etc.  Is there a way to do it in 1 or 2 lines?

By the way, using C# 2.0
0
AxiServices
Asked:
AxiServices
  • 4
  • 3
2 Solutions
 
aibusinesssolutionsCommented:
As far as I know, there is no way to do that without looping through the first array dimension.

What are the results you are looking for, something like this?

myArray(0) = "A"
myArray(0)(0) = "1"

myArray(1) = "B"
myArray(1)(0) = "2"
0
 
aibusinesssolutionsCommented:
Or actually, that should be

myArray(0)(0) = "A"
myArray(0)(1) = "1"

myArray(1)(0) = "B"
myArray(1)(1) = "1"
0
 
aibusinesssolutionsCommented:
I've tried a few different things, and a loop is all I could come up with, sorry.

String s = "A:1,B:2,C:3,D:4";
String[] s1 = s.Split(',');
String[,] myArray = new String[s1.Length,2];
           
for (int i = 0; i < s1.Length; i++)
 {
    String[] s2 = s1[i].Split(':');
    myArray[i,0] = s2[0];
    myArray[i,1] = s2[1];
 }    
0
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
AxiServicesAuthor Commented:
Thanks for the responses so far.  I tried the following using a hashtable instead.  However, I was hoping to use an array.  I have simliar code to the above, but I have several other places where I will need to do the same and am looking for something a little less verbose.

Current code (changed from string array to hashtable):

String s = "A:1,B:2,C:3,D:4";
String[] s1 = s.Split(',');
Hashtable hash = new Hashtable();
            foreach(string country in CountryRates)
                  hash.Add(s.Split(':')[0], s.Split(':')[0])

0
 
AxiServicesAuthor Commented:
Correction to previous post:

String s = "A:1,B:2,C:3,D:4";
String[] s1 = s.Split(',');
Hashtable hash = new Hashtable();
            foreach(string str in s)
                  hash.Add(str.Split(':')[0], str.Split(':')[0])
0
 
AxiServicesAuthor Commented:
Has anyone tried RegEx.split and tried to convert a string into a string[,] ?  Is that even possible?
0
 
angus_young_acdcCommented:
Using RegEx will give you the worst performance overall, and can only really take a single string.  So you would require a foreach etc loop to go through a string[].  

It is possible to do, but very ugly code, and would still require you to iterate through it all (possibly twice).  Your best bet is to just split the string into chars and for loop through them.
0
 
AxiServicesAuthor Commented:
Thanks.  I ended up using a hashtable so that I can easily maneuver around the object and its argument.  I was hoping to simplify the code a bit.  Thanks for the help, though!
0

Featured Post

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now