-
July 12th, 2009, 01:25 PM
#1
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?
-
July 12th, 2009, 01:31 PM
#2
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.
-
July 12th, 2009, 02:11 PM
#3
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
-
July 13th, 2009, 03:01 AM
#4
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.
-
July 13th, 2009, 03:31 AM
#5
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
}
}
-
July 13th, 2009, 03:32 AM
#6
Re: construct array out of string
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
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 13th, 2009, 04:53 AM
#7
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??
}
-
July 13th, 2009, 04:55 AM
#8
Re: construct array out of string
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
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 13th, 2009, 07:15 AM
#9
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.
-
July 13th, 2009, 07:17 AM
#10
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
-
July 13th, 2009, 10:42 AM
#11
Re: construct array out of string
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.
-
July 15th, 2009, 12:21 AM
#12
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.......
-
July 15th, 2009, 12:34 AM
#13
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.......
-
July 15th, 2009, 12:41 AM
#14
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
-
July 15th, 2009, 12:55 AM
#15
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.......
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
|