Poprzedzanie tekstu do początku łańcucha znaków

Jaka jest najszybsza Metoda, aby dodać nową wartość na początku łańcucha znaków?

Author: KyleMit, 2011-05-23

9 answers

var mystr = "Doe";
mystr = "John " + mystr;
Czy to nie zadziała dla Ciebie?
 241
Author: Thor Jacobsen,
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/


Czekaj, zapomniałem uciec z kosmosu.  Wheeeeee [taptaptap]eeeeee.

 136
Author: Gabriele Petrioli,
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

Https://jsperf.com/prepend-text-to-string/1

Tutaj wpisz opis obrazka

 39
Author: KyleMit,
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}`;
 15
Author: Griffi,
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")
 8
Author: chirag,
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}`;
 6
Author: Ilan Schemoul,
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(' ');
 2
Author: MatayoshiMariano,
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)
 0
Author: Mayank,
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 ');
 0
Author: Kachailo Dmytro,
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