728x90
백엔드로부터 데이터를 받았는데
1234567로 와서 하이픈을 붙여서 123-4567로 표시 하고 싶을 때
바로 써 먹을 수 있는 코드.
가져다 많이 많이 써주십쇼
const displayPostCode = (str: string) => {
return `${str.substring(0,3)-${str.substring(3)}}`
}
이상.
substring이 궁금하신 분들은 MDN가서 보세요~
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/substring
String.prototype.substring() - JavaScript | MDN
The substring() method returns the part of the string from the start index up to and excluding the end index, or to the end of the string if no end index is supplied.
developer.mozilla.org
728x90
'자바스크립트' 카테고리의 다른 글
배열의 값을 더해야 할 때 (0) | 2023.03.15 |
---|---|
자바스크립트 날짜바꾸기 (0) | 2023.03.14 |
은근 많이 쓰이는 천 단위 콤마 (0) | 2023.03.14 |
스크립트 < script> (0) | 2022.05.01 |
주석 단축키 (0) | 2022.04.21 |