Programming/JavaScript

[JavaScript] ํŠน์ • ๋ฌธ์ž์—ด ์ œ๊ฑฐํ•˜๋Š” ๋ฐฉ๋ฒ•

yuri lee 2023. 1. 9. 22:08
๋ฐ˜์‘ํ˜•

Intro

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

 

How to solve the problem

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์—์„œ ํŠน์ • ๋ฌธ์ž์—ด์„ ์ œ๊ฑฐํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ๋‹ค์–‘ํ•œ๋ฐ์š”, ๊ทธ ์ค‘์—์„œ ์ €๋Š”  replace() ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํŠน์ • ๋ฌธ์ž์—ด์„ ์ œ๊ฑฐํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.  replace()ํ•จ์ˆ˜๋Š” ํŠน์ • ๋ฌธ์ž์—ด์ด ์กด์žฌํ•˜๊ฑฐ๋‚˜ ์ •๊ทœ์‹์— ๋งŒ์กฑํ•  ๊ฒฝ์šฐ, ์›ํ•˜๋Š” ๋ฌธ์ž์—ด๋กœ ๋Œ€์ฒดํ•ด ์ค๋‹ˆ๋‹ค. 

const str = 'APPLE';

console.log(str.replace('A', '')); // ๋ฌธ์ž์—ด 'A'๋ฅผ ๋นˆ ๊ฐ’์œผ๋กœ ๋Œ€์ฒด, result: PPLE
console.log(str.slice('L', '')); // ๋ฌธ์ž์—ด L'๋ฅผ ๋นˆ ๊ฐ’์œผ๋กœ ๋Œ€์ฒด, result: APPE
๋ฐ˜์‘ํ˜•