文字列の先頭にテキストを入力します
-
09-09-2020 - |
質問
文字列の先頭に新しい値を追加するための最速の方法とは何ですか?
解決
var mystr = "Doe";
mystr = "John " + mystr;
.
これはあなたのために仕事をしないでしょうか?
他のヒント
このようにすることができます..
var mystr = 'is my name.';
mystr = mystr.replace (/^/,'John ');
console.log(mystr);
.
免責事項: http://xkcd.com/208/
問題は早くメソッドとは何ですか。私はいくつかのPerf Metricsを追加すると思いました。
tl; DR 勝者は、幅広い余白で、+
演算子、 を使用しないでください
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)
所属していません StackOverflow