|
-
July 15th, 2009, 12:58 AM
#16
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"
Rob
-
Ohhhhh.... Old McDonald was dyslexic, E O I O EEEEEEEEEE.......
-
July 15th, 2009, 02:16 AM
#17
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.
Rob
-
Ohhhhh.... Old McDonald was dyslexic, E O I O EEEEEEEEEE.......
-
July 15th, 2009, 06:39 PM
#18
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.
Rob
-
Ohhhhh.... Old McDonald was dyslexic, E O I O EEEEEEEEEE.......
-
July 16th, 2009, 09:16 AM
#19
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.
-
July 16th, 2009, 11:57 AM
#20
Re: construct array out of string
 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.
-
July 16th, 2009, 12:38 PM
#21
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
}
}
win7 x86, VS 2008 & 2010, C++/CLI, C#, .NET 3.5 & 4.0, VB.NET, VBA... WPF is comming
remeber to give feedback  you think my response deserves recognition? perhaps you may want to click the Rate this post link/button and add to my reputation
private lessons are not an option so please don't ask for help in private, I won't replay
if you use Opera and you'd like to have the tab-button functionality for the texteditor take a look at my Opera Tab-UserScirpt; and if you know how to stop firefox from jumping to the next control when you hit tab let me know
-
July 16th, 2009, 01:02 PM
#22
Re: construct array out of string
 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];
}
 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.
Last edited by monalin; July 16th, 2009 at 01:09 PM.
-
July 16th, 2009, 01:48 PM
#23
Re: construct array out of string
 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
win7 x86, VS 2008 & 2010, C++/CLI, C#, .NET 3.5 & 4.0, VB.NET, VBA... WPF is comming
remeber to give feedback  you think my response deserves recognition? perhaps you may want to click the Rate this post link/button and add to my reputation
private lessons are not an option so please don't ask for help in private, I won't replay
if you use Opera and you'd like to have the tab-button functionality for the texteditor take a look at my Opera Tab-UserScirpt; and if you know how to stop firefox from jumping to the next control when you hit tab let me know
-
July 16th, 2009, 01:55 PM
#24
Re: construct array out of string
 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.
-
July 16th, 2009, 02:02 PM
#25
Re: construct array out of string
 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 
 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
win7 x86, VS 2008 & 2010, C++/CLI, C#, .NET 3.5 & 4.0, VB.NET, VBA... WPF is comming
remeber to give feedback  you think my response deserves recognition? perhaps you may want to click the Rate this post link/button and add to my reputation
private lessons are not an option so please don't ask for help in private, I won't replay
if you use Opera and you'd like to have the tab-button functionality for the texteditor take a look at my Opera Tab-UserScirpt; and if you know how to stop firefox from jumping to the next control when you hit tab let me know
-
July 16th, 2009, 02:22 PM
#26
Re: construct array out of string
 Originally Posted by memeloo
could you point me to this features, I'd like to experiment 
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
-
July 16th, 2009, 08:23 PM
#27
Re: construct array out of string
 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.
Rob
-
Ohhhhh.... Old McDonald was dyslexic, E O I O EEEEEEEEEE.......
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|