Solved

String Replace and Search Filter

Posted on 2011-02-22
8
426 Views
Last Modified: 2012-08-14
Hi i wrote a class file

in my class file am trying to achieve like this

but am getting this error :String cannot be of zero length.

 if (username.Contains("."))
            {
                string Fname = username.Substring(0, username.IndexOf("."));
                Fname = Fname.Replace("", " ");
                string[] splitString = username.Split(new char[] { '.' });
                username = splitString[splitString.Length - 1];
               
            }

  if (username.IndexOf(" ") > 0)
                    search.Filter = "(cn=" + username + ")";
                else
                    search.Filter = "(sn=" + username + ")" & "(givenname=" + Fname + "));


in this case,if i pass a string like Joe will.smith

then

My first name should be : Joewill (it should eliminate the space,if space is there,if not leave it as it is)
lastname=smith

then i will use the search filter against "sn" and "givenname" retrieve the user info.

Please correct my mistakes,its throwing an error.
0
Comment
Question by:Sha1395
[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
  • 5
  • 2
8 Comments
 
LVL 12

Expert Comment

by:Mamtha1982
ID: 34958387
string[] names;
if (username.Contains("."))
            {
                names = username.Split(new char[] {'.'},StringSplitOptions.RemoveEmptyEntries);
                names[0] =  names[0].Replace(" ", "");
            }

//Now names[0] is first name and names[1] is last name
0
 

Author Comment

by:Sha1395
ID: 34960135
Hi Mamtha,

Thanks for your response,how can i pass first name and last name in to my search filter

something like this

search.Filter = "(sn=" + username + ")" & "(givenname=" + Fname + "));
0
 
LVL 11

Expert Comment

by:Sudhakar Pulivarthi
ID: 34960669
Hi,
Try this code:

string first, last;
            string data = "Joe will.smith";

            string newdata = data.Replace(" ", ""); // Remove spaces.

            char[] sep = {'.'};
            string[] words = newdata.Split(sep, StringSplitOptions.RemoveEmptyEntries);

            if (words.Length != 2)
            {
                // Error.
            }
            else
            {
                first = words[0];
                last = words[1];
            }
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 12

Expert Comment

by:Mamtha1982
ID: 34962437
Something like this(not sure about your Filter syntax):

string[] names;
if (username.Contains("."))
            {
                names = username.Split(new char[] {'.'},StringSplitOptions.RemoveEmptyEntries);
                names[0] =  names[0].Replace(" ", "");
            }

//Now names[0] is first name and names[1] is last name
search.Filter = "(sn=" + names[0] + ")" + " (givenname=" + names[1] + ")";
0
 

Author Comment

by:Sha1395
ID: 34965386
thanks Mamtha,

your replace condition works perfectly,but when i pass name[0] and names[1] in my search filter it throws error like


search.Filter = "(sn=" + names[0] + ")(givenname=" + names[1] + ")";


Error: use of unassigned local variable "names".

am not sure why its not recoganized
0
 

Author Comment

by:Sha1395
ID: 34965564
Here is the modified code but still i can't pass the Fname to my search filter its thowing error :

"The name (fname) doesn't exist in the current context"

 if (username.Contains("."))
            {
                string Fname = username.Substring(0, username.IndexOf("."));
                Fname = Fname.Replace(" ", "");

                string[] splitString = username.Split(new char[] { '.' });
                username = splitString[splitString.Length - 1];

}


if (username.IndexOf(" ") > 0)

                    search.Filter = "(cn=" + username + ")";

                else

                      search.Filter = "(sn=" + username + ")(givenname=" + Fname + ")";

Open in new window

0
 

Accepted Solution

by:
Sha1395 earned 0 total points
ID: 34966501
Figured out by my own

if (username.Contains("."))
                {
                    string Fname = username.Substring(0, username.IndexOf("."));
                    Fname = Fname.Replace(" ", "");
                    
                    string[] splitString = username.Split(new char[] { '.' });
                    username = splitString[splitString.Length - 1];

search.Filter = "(sn=" + username + ")&(givenname=" + Fname + ")";
}
  if (username.IndexOf(" ") > 0)

                    search.Filter = "(cn=" + username + ")";

Open in new window

0
 

Author Closing Comment

by:Sha1395
ID: 34995575
it works like a charm
0

Featured Post

Independent Software Vendors: 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

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.
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

756 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