CopyPastor

Detecting plagiarism made easy.

Score: 0.811377443167703; Reported for: String similarity Open both answers

Possible Plagiarism

Plagiarized on 2022-06-08
by Matte

Original Post

Original - Posted on 2009-06-22
by Steve Harrison



            
Present in both answers; Present only in the new answer; Present only in the old answer;

The basic solution is:
function capitalizeFirstLetter(string) { return string.charAt(0).toUpperCase() + string.slice(1); } console.log(capitalizeFirstLetter('foo')); // Foo
Some other answers modify String.prototype (this answer used to as well), but I would advise against this now due to maintainability (hard to find out where the function is being added to the prototype and could cause conflicts if other code uses the same name / a browser adds a native function with that same name in future).
...and then, there is so much more to this question when you consider internationalisation, as this astonishingly good answer (buried below) shows.
If you want to work with Unicode code points instead of code units (for example to handle Unicode characters outside of the Basic Multilingual Plane) you can leverage the fact that String#[@iterator] works with code points, and you can use toLocaleUpperCase to get locale-correct uppercasing:
const capitalizeFirstLetter = ([ first, ...rest ], locale = navigator.language) => first.toLocaleUpperCase(locale) + rest.join('') console.log( capitalizeFirstLetter('foo'), // Foo capitalizeFirstLetter("πΆπ²π‘ŒπΌπ²π‘‰"), // "πŽπ²π‘ŒπΌπ²π‘‰" (correct!) capitalizeFirstLetter("italya", 'tr') // Δ°talya" (correct in Turkish Latin!) )

The basic solution is:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
function capitalizeFirstLetter(string) { return string.charAt(0).toUpperCase() + string.slice(1); }
console.log(capitalizeFirstLetter('foo')); // Foo
<!-- end snippet -->
Some other answers modify `String.prototype` (this answer used to as well), but I would advise against this now due to maintainability (hard to find out where the function is being added to the `prototype` and could cause conflicts if other code uses the same name / a browser adds a native function with that same name in future).
...and then, there is so much more to this question when you consider internationalisation, as [this astonishingly good answer][1] (buried below) shows.
If you want to work with Unicode code points instead of code units (for example to handle Unicode characters outside of the Basic Multilingual Plane) you can leverage the fact that `String#[@iterator]` works with code points, and you can use `toLocaleUpperCase` to get locale-correct uppercasing:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const capitalizeFirstLetter = ([ first, ...rest ], locale = navigator.language) => first.toLocaleUpperCase(locale) + rest.join('')
console.log( capitalizeFirstLetter('foo'), // Foo capitalizeFirstLetter("πΆπ²π‘ŒπΌπ²π‘‰"), // "πŽπ²π‘ŒπΌπ²π‘‰" (correct!) capitalizeFirstLetter("italya", 'tr') // Δ°talya" (correct in Turkish Latin!) )
<!-- end snippet -->
For even more internationalization options, please see the [original answer below][1].



[1]: https://stackoverflow.com/a/53930826/38522

        
Present in both answers; Present only in the new answer; Present only in the old answer;