문제

문자열의 시작 부분에 새 값을 추가하려면 가장 빠른 방법은 무엇입니까?

도움이 되었습니까?

해결책

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

이 작업을 수행하지 않습니까?

다른 팁

당신은 그것을 이렇게 할 수 있습니다 ..

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

console.log(mystr);
.

면책 조항 : http://xkcd.com/208/


기다려, 공간을 벗어나는 것을 잊어 버렸습니다. Wheeeeeeeeeeeeeeee.

질문은 가장 빠른 방법이 무엇인지에 관한 것입니다. 나는 Perf 메트릭을 추가 할 것으로 생각했습니다.

tl; 박사 넓은 마진에 의해 승자가, 을 사용하지 마십시오

https://jsperf.com/prend-text-to-string/1 <./ a>

여기에 이미지 설명을 입력하십시오 >

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