Click here to Skip to main content
15,122,367 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I want to separate and capitalize two word from first name and last name.

Input: Dennis bergkamp

Expecting Output: DB

What I have tried:

I have to integrate here,

C#
recordElement.Add(new XElement("SigneeInitial", string.Format("{2}", input.FirstName, input.LastName, "here code for capitalize two word")));
Posted
Updated 11-Oct-21 21:36pm
v2
Comments
PIEBALDconsult 12-Oct-21 9:17am
   
Only the user can tell how he wants his name capitalized.

You could write a method that can create the expected output:
C#
private string CapitalizeName(string firstName, string lastName)
{
	return $"{firstName[0]}{lastName[0]}".ToUpper();
}

This method can be called like this:
C#
CapitalizeName(input.FirstName, input.LastName)
   
Comments
Member 12955507 12-Oct-21 2:32am
   
sir I have applied like this.
            recordElement.Add(new XElement("SigneeInitial", input.FirstName.Substring(0, 1).ToUpper() + input.LastName.Substring(0, 1).ToUpper()));


good or bad?
lmoelleb 12-Oct-21 4:01am
   
It will work, bug cramming everything into one line serves two purposed: 1) Makes you feel clever, 2) Makes the code just a bit more unreadable. The best thing to do as a programmer is to accept you are not clever (we get reminded every day as we work) and keep the simple easy to read - which means each line preferably should do one thing. So I would make a method as suggested, or at least assign it to a variable (with a proper name) and then create the XML on the next line.
There is no standard function for capitalization, but it's pretty easy to write one.
The most performant version is this (from when I tested them a long time ago):
C#
static string CapitalizeFirstWord(string s)
    {
    if (string.IsNullOrWhitespace(s))
       {
       return "";
       }
    char[] a = s.ToCharArray();
    a[0] = char.ToUpper(a[0]);
    return new string(a);
    }


But ... it's not a good idea to do it. You should treat people's names as accurate when given as changing them can give offense, and some names are more complicted than "John Smith". Examples would include
John Smith the Third
John von Smith 
John Von Smith
John deSmith
John DeSmith
John ffoulkes-Smith
John Double-Barrelled
John O'Smith
There are loads of other prefixes as well: "el" from the Spanish, "le" and "la" from French, ... the list goes on.

The only way to tell how a name should be capitalised is to ask the "owner" - and you did that when they input the name to start with!
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900