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
SFUserSFUser 

How to convert a string to "capitalize" in LWC

hi all..,
my requirement is...

we have string value and that should be convert to "capitalize" in the LWC component..

For eg:

string = 'testing sf user"
i want to convet that string into "Testing Sf User"

thanks in advance..

Best Answer chosen by SFUser
AnkaiahAnkaiah (Salesforce Developers) 
Hi ,

try with below code.
String rep_name = 'testing sf user';
List<String> elems = rep_name.split(' ');
rep_name = '';
for (String x : elems)
{
    rep_name += x.substring(0,1).toUpperCase()+x.substring(1,x.length()) + ' ';
}
System.debug('>>>'+rep_name);
If this helps, Please mark it as best answer.

Thanks!!
 

All Answers

mukesh guptamukesh gupta
Hi,

Please use below code:-
 
var txtString = 'testing sf user"
console.log('UPPER CASE==>> '+capitalize(txtString)); 

function capitalize(input) {  
    var words = input.split(' ');  
    var CapitalizedWords = [];  
    words.forEach(element => {  
        CapitalizedWords.push(element[0].toUpperCase() + element.slice(1, element.length));  
    });  
    return CapitalizedWords.join(' ');  
}

if you need any assistanse, Please let me know!!

Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh



 
AnkaiahAnkaiah (Salesforce Developers) 
Hi ,

try with below code.
String rep_name = 'testing sf user';
List<String> elems = rep_name.split(' ');
rep_name = '';
for (String x : elems)
{
    rep_name += x.substring(0,1).toUpperCase()+x.substring(1,x.length()) + ' ';
}
System.debug('>>>'+rep_name);
If this helps, Please mark it as best answer.

Thanks!!
 
This was selected as the best answer