분류
javascript
Javascript 30초 Snippet - String : truncateString
본문
문자열을 지정된 길이까지 자릅니다.
https://github.com/30-seconds/30-seconds-of-code#bytesize
문자열의 길이가 num보다 큰지 확인하십시오. 끝에 또는 원래 문자열에 '...'를 추가하여 원하는 길이로 잘린 문자열을 반환합니다.
const truncateString = (str, num) => str.length > num ? str.slice(0, num > 3 ? num - 3 : num) + '...' : str;
ex)
truncateString('boomerang', 7); // 'boom...'
- 이전글Javascript 30초 Snippet - String : unescapeHTML 19.11.25
- 다음글Javascript 30초 Snippet - String : toTitleCase 19.11.25