Solved

Tokenizing a string

Posted on 2004-09-21
13
450 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
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…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …

687 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