Programming

[js] c#과 같은 string.format 구현하기

steloflute 2015. 12. 30. 23:30

http://warmz.tistory.com/entry/js-c%EA%B3%BC-%EA%B0%99%EC%9D%80-stringformat-%EA%B5%AC%ED%98%84%ED%95%98%EA%B8%B0


출처 및 참고 ::

 http://stackoverflow.com/questions/610406/javascript-equivalent-to-printf-string-format


C#의 string.format처럼 js에도 동일한 함수가 있으면 좋겠다고 생각해서 찾아본 결과, 역시 좋은 샘플이 있었다.

1
2
3
4
5
6
7
8
if (!String.prototype.format) {
  String.prototype.format = function () {
    var args = arguments;
    return this.replace(/{(\d+)}/g, function (match, number) {
      return typeof args[number] != 'undefined' ? args[number] : match;
    });
  };
}

1
"Hello, {0}".format("jihyung");





'Programming' 카테고리의 다른 글

파이썬 엑셀 쓰기 라이브러리 비교  (0) 2016.01.07
[JAVA] xlsx(엑셀) 파일 읽고 쓰기  (0) 2016.01.07
cons  (0) 2015.10.15
자바의 Dynamic Proxies  (0) 2015.09.25
HTTP Made Really Easy  (0) 2015.09.25