Hello All,
I am using ProperCase(FirstName) and ProperCase(LastName) for saving the first and last names in our C# Web application.
But for some names like "MacDonald" it is saving as "Macdonald".
How can I resolve this issue?
Please help.
Thank you,
Regards,
CK
Loading
chanikya kondaPosted Feb 3, 2009, 5:41 PM
// Convert only the First char to Upper Case
protected string UpperCaseFirst(string s)
{
if (s.Length==0)
{
return string.Empty;
}
char[] a = s.ToCharArray();
a[0] = char.ToUpper(a[0]);
return new string(a);
}
// Convert the First char to Upper Case and rest of the chars to Lower Case
protected string UpperCaseFirstAndLowerCaseRestOfString(string s)
{
if (s.Length==0)
{
return string.Empty;
}
char[] a = s.ToCharArray();
a[0] = char.ToUpper(a[0]);
for(int i = 1;i
a[i] = char.ToLower(a[i]);
}
return new string(a);
}
protected string ProperCase(string Name)
{
char[] a = Name.ToCharArray();
int NumberOfCapitalLetters = 0;
for(int i = 0;i
if(char.IsUpper(a[i]) && i>0)
{
NumberOfCapitalLetters++;
}
}
if(NumberOfCapitalLetters==1)
{
Name=UpperCaseFirst(Name);
}
else
{
Name=UpperCaseFirstAndLowerCaseRestOfString(Name);
}
return Name;
}
Liam MillarPosted Feb 3, 2009, 3:32 PM
The problem is the ProperCase. Unless there is a non-alpha character eg. a space or a dot between Mac Donald ProperCase will only capitalises the first letter and renders the rest lowercase. ie
ABC, aBC, ABc etc
all become Abc after ProperCase.
but A Bc, A B C, A.B.C
should stay the same.
Maybe put in an extra field for Mc./Mac./none like Mr./Mrs./Ms title. Then let the user type the remainder of thier surname to do ProperCase(LastName) then join the two surname strings as one before saving.
If option none is picked set first string to be empty else which ever Mac option they chose.
Or just direct them to leave a space between Mac Donald etc.
Kapil Deo MalhotraPosted Feb 3, 2009, 3:23 PM
ProperCase means the Title Case so if you use that it will give that output. If you want only the first Letter should be in Caps the covert the First Letter into upercase and keep rest as it as.
Hope that helps