Poprzedzanie tekstu do początku łańcucha znaków
Jaka jest najszybsza Metoda, aby dodać nową wartość na początku łańcucha znaków?
9 answers
var mystr = "Doe";
mystr = "John " + mystr;
Czy to nie zadziała dla Ciebie?Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-03-16 14:15:05
Możesz to zrobić w ten sposób ..
var mystr = 'is my name.';
mystr = mystr.replace (/^/,'John ');
console.log(mystr);
zastrzeżenie: http://xkcd.com/208/
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-06-23 10:36:35
Ponieważ pytanie dotyczy metody najszybszej , pomyślałem, że zwymiotuję dodając kilka wskaźników perf.
TL; DR zwycięzcą jest operator +
i Proszę nigdy nie używać regex
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2019-03-29 01:04:43
ES6:
let after = 'something after';
let text = `before text ${after}`;
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-12-20 14:12:12
Możesz to zrobić również w ten sposób.]}
"".concat("x","y")
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-08-15 11:17:57
Jeśli chcesz używać wersji Javascript o nazwie ES 2015 (aka ES6) lub nowszej, możesz użyć ciągów szablonów wprowadzonych przez ES 2015 i zalecanych przez niektóre wytyczne (np.):
const after = "test";
const mystr = `This is: ${after}`;
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2019-11-11 10:58:57
Inną opcją byłoby użycie join
var mystr = "Matayoshi";
mystr = ["Mariano", mystr].join(' ');
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-09-12 14:25:43
Możesz użyć
var mystr = "Doe";
mystr = "John " + mystr;
console.log(mystr)
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-08-12 03:40:11
EcmaScript 2017 dodał do prototypu string specjalne funkcje. padStart i padEnd to dwie nowe metody dostępne w JavaScript string prototype object. Jak sama nazwa wskazuje, pozwalają na formatowanie ciągu znaków poprzez dodanie znaków dopełniających na początku lub końcu. (Nie obsługiwane przez IE11 i niższe)
var mystr = "Doe";
mystr = mystr.padStart('John ');
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2021-02-07 20:39:44