CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 2 FirstFirst 12
Results 16 to 27 of 27
  1. #16
    Join Date
    Jun 2001
    Location
    Melbourne/Aus (C# .Net 4.0)
    Posts
    686

    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.......

  2. #17
    Join Date
    Jun 2001
    Location
    Melbourne/Aus (C# .Net 4.0)
    Posts
    686

    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.......

  3. #18
    Join Date
    Jun 2001
    Location
    Melbourne/Aus (C# .Net 4.0)
    Posts
    686

    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.......

  4. #19
    Join Date
    Jul 2006
    Posts
    297

    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.

  5. #20
    Join Date
    Jun 2008
    Posts
    2,477

    Re: construct array out of string

    Quote Originally Posted by monalin View Post
    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.

  6. #21
    Join Date
    Oct 2008
    Location
    Cologne, Germany
    Posts
    756

    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

  7. #22
    Join Date
    Jul 2006
    Posts
    297

    Re: construct array out of string

    Quote Originally Posted by BigEd781 View Post
    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 View Post
    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.

  8. #23
    Join Date
    Oct 2008
    Location
    Cologne, Germany
    Posts
    756

    Re: construct array out of string

    Quote Originally Posted by monalin View Post
    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

  9. #24
    Join Date
    Jul 2006
    Posts
    297

    Re: construct array out of string

    Quote Originally Posted by memeloo View Post
    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.

  10. #25
    Join Date
    Oct 2008
    Location
    Cologne, Germany
    Posts
    756

    Re: construct array out of string

    Quote Originally Posted by monalin View Post
    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

    Quote Originally Posted by monalin View Post
    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

  11. #26
    Join Date
    Jul 2006
    Posts
    297

    Re: construct array out of string

    Quote Originally Posted by memeloo View Post
    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

  12. #27
    Join Date
    Jun 2001
    Location
    Melbourne/Aus (C# .Net 4.0)
    Posts
    686

    Re: construct array out of string

    Quote Originally Posted by monalin View Post
    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.......

Page 2 of 2 FirstFirst 12

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured