• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 6655
  • 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
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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