Sha1395
asked on
String Replace and Search Filter
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.Le ngth - 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.
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.Le
}
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.
ASKER
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 + "));
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 + "));
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.RemoveE mptyEntrie s);
if (words.Length != 2)
{
// Error.
}
else
{
first = words[0];
last = words[1];
}
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.RemoveE
if (words.Length != 2)
{
// Error.
}
else
{
first = words[0];
last = words[1];
}
Something like this(not sure about your Filter syntax):
string[] names;
if (username.Contains("."))
{
names = username.Split(new char[] {'.'},StringSplitOptions.R emoveEmpty Entries);
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] + ")";
string[] names;
if (username.Contains("."))
{
names = username.Split(new char[] {'.'},StringSplitOptions.R
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] + ")";
ASKER
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
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
ASKER
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"
"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 + ")";
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
it works like a charm
if (username.Contains("."))
{
names = username.Split(new char[] {'.'},StringSplitOptions.R
names[0] = names[0].Replace(" ", "");
}
//Now names[0] is first name and names[1] is last name