-
construct array out of string
Hey, how can i make an array out of a string which contains something like this:
name:Trainwreck,location:forum,xpos:12ypos:67
So that's my string, now i want to create an array out of this so that i can ask the value from that array by the name. So for example in my array i should be able to do this:
myArray.name; (should give me: Trainwreck)
myArray.xpos; (should give me: 12)
myArray.location; (should give me: forum)
etc.
So my question is, how can i convert a string like that to an array?
-
Re: construct array out of string
Assuming that the string would always look like this, You will have to write a class with public string properties. Then in the constructor of the class, you will pass this string. The constructor will then break the string using String.Split method first using comma and then using colon and put the strings into respective properties.
-
Re: construct array out of string
to be precise it won't be an array but a class. you want to turn you string into a class instance and an array could be used to store those instances:
Code:
((YourClass^)myArray[0]).Name;
((YourClass^)myArray[0]).XPos;
((YourClass^)myArray[0]).Location;
if you decide to use an array and not some typed collection like List etc.
-
Re: construct array out of string
OK i get the idea of splitting the string, but what i don't get is how to create the members of the class.
The format of the string is always the same, but the data in it can be different. So it's not always sending
name:Trainwreck,location:forum,xpos:12ypos:67
It could also be:
ID:12,rank:4,gear:none
So would it still be possible to construct a class, array or list (or what ever is best in this case) when i don't know the exact vars?? In PHP i could do something like this with a 2D array. Maybe this is possible in C# too??
//When the string is splitted
string s1 = myArray[0]["name"];
string s2 = myArray[0]["gear"];
string s3 = myArray[0]["location"];
etc.
-
Re: construct array out of string
First, I would definitely try my best to normalize that data into one format if possible. Otherwise, your class(es) will need to know the format somehow.
Code:
class MyClass
{
public MyClass( string data )
{
// parse the string and assign fields
}
}
-
Re: construct array out of string
Quote:
Originally Posted by
Trainwreck
So would it still be possible to construct a class, array or list (or what ever is best in this case) when i don't know the exact vars?? In PHP i could do something like this with a 2D array. Maybe this is possible in C# too??
//When the string is splitted
string s1 = myArray[0]["name"];
string s2 = myArray[0]["gear"];
string s3 = myArray[0]["location"];
your property names are the ID, rank and gear and values respectively 12, 4 and "none" so constricting a class is very simple
-
Re: construct array out of string
Sorry, but im not sure how to create the class. Does this mean i have to define the member vars in that class, and fill them when splitting the string??
class MyClass
{
string gear;
string location;
string name;
etc.. etc..
public MyClass ( string longString )
{
//split the string and place the values in the right member variables??
}
-
Re: construct array out of string
Quote:
Originally Posted by
Trainwreck
Sorry, but im not sure how to create the class.
I think you need something like this: http://www.csharp-station.com/Tutorials/Lesson07.aspx :D
-
Re: construct array out of string
lol, it's not that i don't know how to work with classes. It's more that i don't know how to create it when i don;t know what kind of member variables there are.
But i think i can do this with a dictionary list.
-
Re: construct array out of string
but you do know what their names are, you've shown us the names:
ID, rank, gear
-
Re: construct array out of string
Quote:
Originally Posted by
Trainwreck
Sorry, but im not sure how to create the class. Does this mean i have to define the member vars in that class, and fill them when splitting the string??
class MyClass
{
string gear;
string location;
string name;
etc.. etc..
public MyClass ( string longString )
{
//split the string and place the values in the right member variables??
}
What you need to do is use a dictionary object inside your class. For this example i'll assume that all the values are strings however you can use whatever data type you want. Assuming your data is always in the same format, " <name>: <value>, .... " this should work for you.
Code:
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
public class MyClass
{
private Dictionary<String, String> _values = null;
public MyClass(String input)
{
ParseInput(input);
}
private void ParseInput(String input)
{
_values = new Dictionary<String, String>();
Regex reg = new Regex(@"(?<name>[^:]+):(?<value>[^,]+),?", RegexOptions.Singleline | RegexOptions.IgnoreCase);
Match m = reg.Match(input);
while(m.Success)
{
_values[m.Groups["name"].Value] = m.Groups["value"].Value;
m = m.NextMatch();
}
}
public String this[String name]
{
get
{
if (_values.ContainsKey(name))
return _values[name];
return String.Empty;
}
}
}
To use this class all you need to do is the following.
Code:
MyClass mc = new MyClass("name:Trainwreck,location:forum,xpos:12,ypos:67");
String name = mc["name"];
....
....
You can actually create a class on the fly if you don't know what properties you are going to have in the class are but this is defiantly a more advanced topic. Which brings on a whole other level of complexity to the scenario. This should work for you since it doesn't require you know the names of the properties at compilation.
-
Re: construct array out of string
Neat answer and also reminded me of when I had a problem and decided to use Regular Expressions... I then had 2 problems ;)
-
Re: construct array out of string
I prefer monolin's way, but here's another technique using a String Extender. This is by no means the neatest version!
Assumes .NET 3.x and above...
Code:
public static string GetValue(this string thisString, String name)
{
int index1 = thisString.IndexOf(name);
if (index1.Equals(-1))
return String.Empty;
index1 += name.Length + 1;
int index2 = thisString.IndexOf(',', index1);
if (index2.Equals(-1))
return thisString.Substring(index1);
return thisString.Substring(index1, index2 - index1);
}
Then in your main programme:
Code:
String a = "name:Trainwreck,location:forum,xpos:12,ypos:67";
String b = a.GetValue("ypos");
String c = a.GetValue("location");
-
Re: construct array out of string
I didn't test it but I think your method will fail if the order of parameters is different and one of the values is the same as the key: (two names)
"location:Name,xpos:12,name:Trainwreck,ypos:67";
-
Re: construct array out of string
Yes mine would fail, however, it maybe fixable by appending a ':' to the 'name' parameter, before the search...? I'll check...
-
Re: construct array out of string
New extender...
Code:
public static string GetValue(this string thisString, String name)
{
name += ':';
int index1 = thisString.IndexOf(name);
if (index1.Equals(-1))
return String.Empty;
index1 += name.Length;
int index2 = thisString.IndexOf(',', index1);
if (index2.Equals(-1))
return thisString.Substring(index1);
return thisString.Substring(index1, index2 - index1);
}
main programme:
Code:
String a = "name:Trainwreck,Trainwreck:forum,xpos:12,ypos:67";
String b = a.GetValue("Trainwreck");
// b = "forum"
-
Re: construct array out of string
All being said, a String Extender is suppose to extend Strings, as in ALL Strings... Mine is for a special case for Strings that are in a certain format. So really, if this is a large project with many Extenders, I don't think it is the way to go.
-
Re: construct array out of string
One more solution using a Class (but not a Dictionary)
Code:
public class KeyValueString
{
private String _keyValueString;
public KeyValueString(String keyValueString)
{
// if (keyValueString is no in the correct format)
// throw an exception
_keyValueString = keyValueString;
}
public KeyValueString(KeyValueString keyValueString)
{
_keyValueString = keyValueString.ToString();
}
public override String ToString()
{
return _keyValueString;
}
public String GetValue(String key)
{
key += ':';
Int32 index1 = _keyValueString.IndexOf(key);
if (index1.Equals(-1))
return String.Empty;
index1 += key.Length;
Int32 index2 = _keyValueString.IndexOf(',', index1);
if (index2.Equals(-1))
return _keyValueString.Substring(index1);
return _keyValueString.Substring(index1, index2 - index1);
}
}
And then in the main....
Code:
KeyValueString a = new KeyValueString("name:Trainwreck,Trainwreck:forum,xpos:12,ypos:67");
String b = a.GetValue("Trainwreck");
String c = a.GetValue("xpos");
I guess if you knew the C# types of the value, given a certain key (eg xpos = Int32), you could write other member functions GetInt32Value, GetDoubleValue etc.
Hope this helped.
-
Re: construct array out of string
The main problem with your solution is that it needs to perform calculations each time the function gets run. I haven't tried it, but i assume it might work. However, you want to avoid these type of functions if possible. There's no reason to not parse the string into a list of name / value pairs i.e Dictionary or NameValueCollection.
-
Re: construct array out of string
Quote:
Originally Posted by
monalin
The main problem with your solution is that it needs to perform calculations each time the function gets run. I haven't tried it, but i assume it might work. However, you want to avoid these type of functions if possible. There's no reason to not parse the string into a list of name / value pairs i.e Dictionary or NameValueCollection.
Well, that is just the method to get the value. You can use it to create a dictionary.
-
Re: construct array out of string
and why not something like this?
Code:
class MyClass
{
private int id;
private string name;
private int rank;
public int Id
{
get { return id; }
}
public int Rank
{
get { return rank; }
}
public string Name
{
get { return name; }
}
public MyClass(string text)
{
// parse the text
}
}
-
Re: construct array out of string
Quote:
Originally Posted by
BigEd781
Well, that is just the method to get the value. You can use it to create a dictionary.
Would be counter productive to create a dictionary like this because he would need to know the all the values ahead of time. I suppose you could use the dictionary as a caching mechanism though and just save the result in a dictionary and only resort to the GetValue() if no entry is in the dictionary... Something like this.
Code:
public String GetCachedValue(String name)
{
if(!dictionary.ContainsKey(name))
dictionary[name] = GetValue(name);
return dictionary[name];
}
Quote:
Originally Posted by
memeloo
and why not something like this?
In the original problem it was stated that the fields present in the string would be different depending on what he was trying to do. So we needed a solution that was a little more generic and worked with any string of name/value pairs.
-
Re: construct array out of string
Quote:
Originally Posted by
monalin
In the original problem it was stated that the fields present in the string would be different depending on what he was trying to do. So we needed a solution that was a little more generic and worked with any string of name/value pairs.
oh, sorry, I must have overseen it... anyway it would be cool if it was possible to create classes/structures dynamicaly, at least their properties
-
Re: construct array out of string
Quote:
Originally Posted by
memeloo
oh, sorry, I must have overseen it... anyway it would be cool if it was possible to create classes/structures dynamicaly, at least their properties
It is possible to create a class/structure using some of the more advanced features of C#. But that's very overkill for this problem hah. All the required tools are in the .NET framework to create a new class and add properties etc... I'm trying to think of a reason why I would want to do that though.
-
Re: construct array out of string
Quote:
Originally Posted by
monalin
It is possible to create a class/structure using some of the more advanced features of C#.
could you point me to this features, I'd like to experiment :D
Quote:
Originally Posted by
monalin
I'm trying to think of a reason why I would want to do that though.
maybe not this time but it's good to know there is such possibility just in case :p
-
Re: construct array out of string
Quote:
Originally Posted by
memeloo
could you point me to this features, I'd like to experiment :D
I don't have time to write up a good example but i was able to find a link that explains how its done. Defiantly an interesting concept but like the author says, its slow performance wise. I'm sure you can understand why it would be.
http://mironabramson.com/blog/post/2008/06/Create-you-own-new-Type-and-use-it-on-run-time-(C).aspx
-
Re: construct array out of string
Quote:
Originally Posted by
monalin
The main problem with your solution is that it needs to perform calculations each time the function gets run. I haven't tried it, but i assume it might work. However, you want to avoid these type of functions if possible. There's no reason to not parse the string into a list of name / value pairs i.e Dictionary or NameValueCollection.
Agreed, I just posted it as an alternative way. I guess, it would depend on how many times a given KeyValueString was going to be accessed before being discarded. Many times, then a Dictionary would be quicker, a couple, then maybe a parsed string.