Solved

Tokenizing a string

Posted on 2004-09-21
13
414 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Are either of those more efficient than my example?  I guess it would increase code readability, but would add quite a few lines...
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 15

Expert Comment

by:praneetha
Comment Utility
i guess so
0
 
LVL 5

Assisted Solution

by:tzxie2000
tzxie2000 earned 150 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

743 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

17 Experts available now in Live!

Get 1:1 Help Now