Solved

Tokenizing a string

Posted on 2004-09-21
13
440 Views
Last Modified: 2010-04-15
I'm looking for the most streamlined method to token a string like this into variables:
fullName=John Doe;ou=Accounting;mail=john@example.com;telephoneNumber=123-4560;cn=JDoe

I want to place the values in their own variables.  For example:
string fullName = "John Doe";
string ou = "Accounting";
...etc...

How can I do this programmatically?  Here is what I came up with, although it doesn't seem to be the most efficient way to do it.  Something like this:

public static void Main (string[] args)
{
   string test = "fullName=John Doe;ou=Accounting;mail=john@example.com;telephoneNumber=123-4560;cn=JDoe";
   string ou = GetField(test, "ou");
   string mail = GetField(test, "mail");
   string telephoneNumber = GetField(test, "telephoneNumber");
   // and so on...
}
private static string GetField (string source, string var)
{
   string delimiter = ";";
   return source.Substring(source.IndexOf(var), source.IndexOf(delimiter, source.IndexOf(var))-source.IndexOf(var));
}

This works, but is there a better, more efficient way to do this?
0
Comment
Question by:hendridm
  • 4
  • 3
  • 3
  • +1
13 Comments
 
LVL 15

Expert Comment

by:praneetha
ID: 12114785
string[] temp1=test.split(';');

for(int i=0;i<temp1.length;i++)
{
     string[] temp2=temp1[].split('=');
     temp2[0] - fullName
    temp2[1] - John doe
}
0
 
LVL 8

Author Comment

by:hendridm
ID: 12114897
Unfortunately, the data source doesn't always return all the fields.  For example, it might return any of the following:

fullName=John Doe;ou=Accounting;mail=john@example.com;telephoneNumber=123-4560;cn=JDoe
fullName=Jane Doe;ou=Maintenance;mail=jane@example.com;cn=JDoe2
fullName=Jim Smith;cn=JSmith

So your example would create a predictable array.  For example, the 4th index may or may not contain the telephone number.
0
 
LVL 15

Assisted Solution

by:praneetha
praneetha earned 100 total points
ID: 12114972
if this is the string

fullName=Jane Doe;ou=Maintenance;mail=jane@example.com;cn=JDoe2

for(int i=0;i<temp1.length;i++)
{
     string[] temp2=temp1[].split('=');
     temp2[0] - fullName
    temp2[1] - John doe
}

temp1[0]="fullname=jane"
temp1[1]="ou=maintenance"
temp1[2]="mail=jane@example.com"
temp1[3]="cn=JDOe2"

and in a similar way
fullName=Jim Smith;cn=JSmith

temp1[0]="fullname=jim smith"
temp1[1]="cn=jsmith"
for(int i=0;i<temp1.length;i++)
{
     string[] temp2=temp1[].split('=');
switch(temp2[0])
{
case fullname:
string fullnametxt=temp2[1];
break;

case ou:
string outxt=temp2[1]

}
 }

well this is how .. i would do..



0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 5

Expert Comment

by:tzxie2000
ID: 12115075
public static void Main (string[] args)
{
   string test = "fullName=John Doe;ou=Accounting;mail=john@example.com;telephoneNumber=123-4560;cn=JDoe";
   string [] fields=SplitVar(test)
   string res=GetField(fields,"fullName");//or change to others
}

string [] SplitVar(string tosplit)
{
  return tosplit.split(';');
}

string GetField(string[] fields,string fieldname)
{
   int i;
 
   for(i=0;i<fields.Length;i++)
   {
       string []temp=fields[i].Split('=');
       if(temp[0].Equals(fieldname)) break;
   }
   if (i<fields.Length) return temp[1];
   return "";
}
0
 
LVL 15

Expert Comment

by:praneetha
ID: 12115289
i think tzxie200 is optimal in # of lines of code..

but mine is better for performance...

bcz i don't loop thru again and again...evrytime there is getfield("fullname")

instead it only loops thru once and reads the necessary fields...

Thank you
0
 
LVL 8

Author Comment

by:hendridm
ID: 12116656
Are either of those more efficient than my example?  I guess it would increase code readability, but would add quite a few lines...
0
 
LVL 15

Expert Comment

by:praneetha
ID: 12116697
i guess so
0
 
LVL 5

Assisted Solution

by:tzxie2000
tzxie2000 earned 150 total points
ID: 12116776
a little improve of the code
public static void Main (string[] args)
{
   string test = "fullName=John Doe;ou=Accounting;mail=john@example.com;telephoneNumber=123-4560;cn=JDoe";
   string [] fields=SplitVar(test)
   string res=GetField(fields,"fullName");//or change to others
}

string [] SplitVar(string tosplit)
{
  return tosplit.split(';=');
}

string GetField(string[] fields,string fieldname)
{
   int i;
 
   for(i=0;i<fields.Length;i+=2)
   {
       if(fields[i].Equals(fieldname)) break;
   }
   if (i<fields.Length) return fields[i];
   return "";
}

and then
if you really very need the performance I suggest you to use HashCode
0
 
LVL 5

Expert Comment

by:tzxie2000
ID: 12116804
and the efficient is not depend on the code you write

in your code you have to do 4 times indexof for each getfield
it is not a good idea I think

we may split the string first


0
 
LVL 5

Accepted Solution

by:
tomasX2 earned 250 total points
ID: 12118694
I think your original code was pretty good...
it´s probably the most efficient so far...

but I modified it somewhat to account for two things....

Now the GetField() Method only returns only the value
so GetField(test,"ou" would return "Accounting" and not "ou=Accounting"
Don´t know if thats what you wanted but it made more sense to me...

also you code would not handle retrieving the "ou" properly with the following string

"fullName=John Dou;ou=Accounting;mail=john@example.com;telephoneNumber=123-4560;cn=JDoe";

where the full name is John Dou and not Doe... because it would look for the first "ou" instead of "ou="


        public static void Test()
        {
            string test = "fullName=John Doe;ou=Accounting;mail=john@example.com;telephoneNumber=123-4560;cn=JDoe";
            string ou = GetField(test, "ou");
            string mail = GetField(test, "mail");
            string other = GetField(test, "other");
            string telephoneNumber = GetField(test, "telephoneNumber");
            // and so on...
        }

        private static string GetField(string source, string var)
        {
            var += "=";
            if (source.IndexOf(var) != -1)
            {                    
                string delimiter = ";";
                int beginning = source.IndexOf(var) + var.Length;
                return source.Substring(beginning, source.IndexOf(delimiter, beginning) - beginning);
            }
            return "";
        }
0
 
LVL 5

Expert Comment

by:tomasX2
ID: 12118715
Could also add support for case insensitivity.

        private static string GetField(string source, string var)
        {
            var += "=";
            int start = source.ToLower().IndexOf(var.ToLower());
            if ( start != -1)
            {                    
                string delimiter = ";";
                int beginning = start + var.Length;
                return source.Substring(beginning, source.IndexOf(delimiter, beginning) - beginning);
            }
            return "";
        }
0
 
LVL 8

Author Comment

by:hendridm
ID: 12122425
Thanks peeps.  I will take a look at each and award points accordingly.  Since I got such a good return, I'll likely increase the points to 500 so the split isn't so harsh.

Cheers,
Daniel
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question