function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
angusgrantangusgrant 

Make first Letter for FirstName / LastName Upper case apex visualforce page

How can i convert the firstname and Lastname first character to uppercase?

Sorry I am not a Java coder so am not sure how this is done.

Best Answer chosen by Admin (Salesforce Developers) 
JmBessonartJmBessonart

Hi,

  You can create a new method that change the first letter to upper case. Something like this

 

public String nameFormat (String name) {
String result = '';

if (name != null && name != '') {
for (String iter : name.split('[ ]+')) {
if (iter != null && iter != '') {
if (iter.length() > 1) {
result += iter.substring(0,1).toUpperCase() + iter.substring(1,iter.length()) + ' ';
}
else {
result += iter.substring(0,1).toUpperCase() + ' ';
}
}
}
result = result.substring(0, result.length() - 1);
}
return result;
}

 

Example: 

 

nameFormat('foo') >>> return 'Foo'

 

nameFormat('foo and more foo') >>> return 'Foo And More Foo'

 

 

I hope it help you :D

 

J

All Answers

JmBessonartJmBessonart

Hi,

  You can create a new method that change the first letter to upper case. Something like this

 

public String nameFormat (String name) {
String result = '';

if (name != null && name != '') {
for (String iter : name.split('[ ]+')) {
if (iter != null && iter != '') {
if (iter.length() > 1) {
result += iter.substring(0,1).toUpperCase() + iter.substring(1,iter.length()) + ' ';
}
else {
result += iter.substring(0,1).toUpperCase() + ' ';
}
}
}
result = result.substring(0, result.length() - 1);
}
return result;
}

 

Example: 

 

nameFormat('foo') >>> return 'Foo'

 

nameFormat('foo and more foo') >>> return 'Foo And More Foo'

 

 

I hope it help you :D

 

J

This was selected as the best answer
angusgrantangusgrant

Hi thanks for providing this for me.

 

i actually managed to get to the answer a bit quicker than I got your post. However I can see that your method would be better if the applicant have double barelled name etc.. 

 

 

String FirstName = Contact.FirstName; String LastName = Contact.LastName; FirstName = FirstName.substring(0,1).toUpperCase() + FirstName.substring(1).toLowerCase(); LastName = LastName.substring(0,1).toUpperCase() + LastName.substring(1).toLowerCase();

 

 

 

JmBessonartJmBessonart

Yes, also you have to be careful when the string is null , empty or have only one char.

 

Example:

 

Contact.firstName = 'a';

Contact.lastName = 'b';

 

Throw an error with the index of the substring method :

FirstName.substring(1)<<< list index out of bounds ;)

Contact.firstName = null;

Contact.lastName = null;

 

Throw an error :

FirstName.substring(0,1)<<< Attempt to de-reference a null object

Regards

J.

Message Edited by JmBessonart on 06-11-2009 08:52 AM
angusgrantangusgrant
Thankyou for making me aware of these issues. Ammended code below

String FirstName = Contact.FirstName; String LastName = Contact.LastName; if (FirstName != null && LastName != null && FirstName != '' && LastName != ''){ if (FirstName.length() > 1 && LastName.length() > 1) { FirstName = FirstName.substring(0,1).toUpperCase() + FirstName.substring(1).toLowerCase(); LastName = LastName.substring(0,1).toUpperCase() + LastName.substring(1).toLowerCase(); } //creat a validation rule to make sure Firsname and Lastname is greater than 1 character in sf } //we dont need an else statment here as the try catch block around the Contact should catch this error

 

:smileyhappy:
Nilesh Patil 95Nilesh Patil 95
I want same uppercase lowercase but my issue is , my name formate like that ' John-sinha Roy ' expected 'John-Sinha Roy 'what should I do ya