質問

文字列の先頭に新しい値を追加するための最速の方法とは何ですか?

役に立ちましたか?

解決

var mystr = "Doe";
mystr = "John " + mystr;
.

これはあなたのために仕事をしないでしょうか?

他のヒント

このようにすることができます..

var mystr = 'is my name.';
mystr = mystr.replace (/^/,'John ');

console.log(mystr);
.

免責事項: http://xkcd.com/208/


待って、スペースを脱出するのを忘れました。wheeeeee [taptaptap] eeeeee

問題は早くメソッドとは何ですか。私はいくつかのPerf Metricsを追加すると思いました。

tl; DR 勝者は、幅広い余白で、+演算子、 を使用しないでください

https://jsperf.com/prepend-text-tring/1

 Enter Imageの説明がここに

ES6:

let after = 'something after';
let text = `before text ${after}`;

you could also do it this way

"".concat("x","y")

If you want to use the version of Javascript called ES 2015 (aka ES6) or later, you can use template strings introduced by ES 2015 and recommended by some guidelines (like Airbnb's style guide):

const after = "test";
const mystr = `This is: ${after}`;

Another option would be to use join

var mystr = "Matayoshi";
mystr = ["Mariano", mystr].join(' ');

You can use

var mystr = "Doe";
mystr = "John " + mystr;
console.log(mystr)
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top