Programming/JavaScript

[JavaScript] ๊ธด ๋ฌธ์ž์—ด ์ž๋ฅด๊ธฐ, ํŠน์ • ๋ฌธ์ž์—ด ์ˆ˜๊นŒ์ง€ ํ‘œ์‹œํ•˜๋Š” ๋ฐฉ๋ฒ•

yuri lee 2023. 3. 13. 23:23
๋ฐ˜์‘ํ˜•

Intro

์•ˆ๋…•ํ•˜์„ธ์š”. ์ด๋ฒˆ ์‹œ๊ฐ„์—๋Š” ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋กœ ํŠน์ • ๋ฌธ์ž์—ด ์ˆ˜๊นŒ์ง€ ํ‘œ์‹œํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ์•Œ์•„๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. 

 

How to solve the problem

๊ธด ๋ฌธ์ž์—ด์„ ์ž๋ฅด๋Š” ๋ฐฉ๋ฒ•์€ ๋‹ค์–‘ํ•œ๋ฐ์š”, ๋ณดํ†ต ์ƒ๋žต (...)์ฒ˜๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ค„๋ฐ”๊ฟˆ์„ ๋ฐฉ์ง€ํ•˜๊ณค ํ•ฉ๋‹ˆ๋‹ค. ์ €์˜ ์ผ€์ด์Šค์˜ ๊ฒฝ์šฐ ํŠน์ • ๋ฌธ์ž์—ด ์ˆ˜๊นŒ์ง€๋งŒ ํ‘œ์‹œํ•˜๊ณ  ์‹ถ์—ˆ๊ธฐ ๋•Œ๋ฌธ์— ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•์„ ์ด์šฉํ–ˆ์Šต๋‹ˆ๋‹ค. 

 

 

๋‹ค์Œ๊ณผ ๊ฐ™์ด ๊ธด ๋ฌธ์ž์—ด์ด ์žˆ๋‹ค๊ณ  ํ•ฉ์‹œ๋‹ค. 

let str = 'Some very long string';

 

ํŠน์ • ๋ฌธ์ž์—ด ์ˆ˜๋ณด๋‹ค ํด ๊ฒฝ์šฐ substring์„ ์ด์šฉํ•˜์—ฌ ํ•ด๋‹น ๋ฌธ์ž์—ด ์ˆ˜๊นŒ์ง€ ๋ฌธ์ž์—ด์„ ์ž˜๋ผ์ค๋‹ˆ๋‹ค. 

if(str.length > 10) str = str.substring(0,10);

 


https://stackoverflow.com/questions/3414916/display-only-10-characters-of-a-long-string

๋ฐ˜์‘ํ˜•