[level 0] 문자열 겹쳐쓰기

2025. 5. 18. 07:22·Daily Logs/Algorithm Practice

function solution(my_string, overwrite_string, s) {
//목표 He + lloWorl + d 

    const preslice = my_string.slice(0,s); //He 
    const backslice = my_string.slice(s+overwrite_string.length); //d

    return preslice + overwrite_string + backslice;
}
  • 주요 문법 String.prototype.slice()
    String 값의 slice() 메서드는 이 문자열의 일부를 추출하여 이를 새로운 문자열로 반환합니다. 원본 문자열은 수정하지 않습니다.
const str = "The quick brown fox jumps over the lazy dog.";

console.log(str.slice(31));  
// Expected output: "the lazy dog."

console.log(str.slice(4, 19));  
// Expected output: "quick brown fox"

console.log(str.slice(-4));  
// Expected output: "dog."

console.log(str.slice(-9, -5));  
// Expected output: "lazy"
  • 헷갈렸던 부분 -> slice(start,end)가 start 와 end 가 문자열을 포함하면서 자르는지
    정답 :
    start: 포함됨 ✅
    end: 포함 안 됨 ❌ (자르기 멈추는 위치)

예)

const str = "abcdef";

console.log(str.slice(1, 4)); // "bcd"

인덱스 문자
0 a
1 b ✅
2 c
3 d
4 e ❌
5 f

즉 start 이상 end 미만 이라고 외우면 되겠다.
또한 slice(start)는 start부터 끝까지 포함

참고 : MDN
깃허브
프로그래머스

'Daily Logs > Algorithm Practice' 카테고리의 다른 글

[level 0] 문자 리스트를 문자열로 변환하기  (0) 2025.05.18
[level 0] 문자열 섞기  (0) 2025.05.18
[level 0] 홀짝 구분하기  (0) 2025.05.18
[level 0] 문자열 돌리기  (0) 2025.05.18
[level 0] 문자열 붙여서 출력하기  (0) 2025.05.18
'Daily Logs/Algorithm Practice' 카테고리의 다른 글
  • [level 0] 문자 리스트를 문자열로 변환하기
  • [level 0] 문자열 섞기
  • [level 0] 홀짝 구분하기
  • [level 0] 문자열 돌리기
Jcob.moon
Jcob.moon
반가워요~ 하루하루 꾸준히 코딩 작성하는 곳입니다 !!
  • Jcob.moon
    Pixelated Thoughts
    Jcob.moon
  • 전체
    오늘
    어제
    • HelloWorld (176) N
      • Daily Logs (125) N
        • TIL (Today I Learned) (64)
        • Algorithm Practice (55)
        • Dev Book Notes (6) N
      • Deep Dives (36)
        • 문제 해결 (Troubleshooting) (3)
        • CS Fundamentals (22)
        • Career Prep (4)
        • Technical Notes (7)
      • Project Log (7)
      • Any (3)
      • Cooperation (4)
        • Github (2)
        • Conventions (1)
        • Git (1)
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 최근 댓글

  • hELLO· Designed By정상우.v4.10.3
Jcob.moon
[level 0] 문자열 겹쳐쓰기
상단으로

티스토리툴바