Get first character of string: Difference between revisions
Jump to navigation
Jump to search
(Created page with " == PHP == Using the [https://www.php.net/manual/en/function.substr.php PHP: substr] function<ref>[https://www.php.net/manual/en/function.substr.php PHP: substr - Manual]</re...") |
mNo edit summary |
||
| Line 5: | Line 5: | ||
<pre> | <pre> | ||
echo | |||
$string = 'apple'; | |||
if (function_exists('mb_substr')) { | |||
echo mb_substr($string, 0, 1, 'UTF-8'); | |||
} | |||
echo substr($string, 0, 1); | |||
// return 'a' | // return 'a' | ||
</pre> | </pre> | ||
Latest revision as of 15:32, 30 October 2024
PHP[edit]
Using the PHP: substr function[1]
not binary safe
$string = 'apple';
if (function_exists('mb_substr')) {
echo mb_substr($string, 0, 1, 'UTF-8');
}
echo substr($string, 0, 1);
// return 'a'
Javascript[edit]
Using charAt() Method[2]
var str = 'apple';
var first_character = str.charAt(0);
console.log("first_character: " + first_character);