CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 27
  1. #1
    Join Date
    Aug 2005
    Posts
    134

    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?

  2. #2
    Join Date
    Jun 2004
    Location
    Kashmir, India
    Posts
    6,808

    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.

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

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

  4. #4
    Join Date
    Aug 2005
    Posts
    134

    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.

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

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

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

    Re: construct array out of string

    Quote Originally Posted by Trainwreck View Post
    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
    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. #7
    Join Date
    Aug 2005
    Posts
    134

    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??
    }

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

    Re: construct array out of string

    Quote Originally Posted by Trainwreck View Post
    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
    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. #9
    Join Date
    Aug 2005
    Posts
    134

    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.

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

    Re: construct array out of string

    but you do know what their names are, you've shown us the names:

    ID, rank, gear
    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. #11
    Join Date
    Jul 2006
    Posts
    297

    Re: construct array out of string

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

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

    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
    Rob
    -
    Ohhhhh.... Old McDonald was dyslexic, E O I O EEEEEEEEEE.......

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

    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");
    Rob
    -
    Ohhhhh.... Old McDonald was dyslexic, E O I O EEEEEEEEEE.......

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

    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";
    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

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

    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...
    Rob
    -
    Ohhhhh.... Old McDonald was dyslexic, E O I O EEEEEEEEEE.......

Page 1 of 2 12 LastLast

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