Solved

Tokenizing a string

Posted on 2004-09-21
13
426 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

911 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now