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.