Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Tokenizing a string

Posted on 2004-09-21
13
Medium Priority
?
462 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 400 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
Industry Leaders: 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 600 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 1000 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

609 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