String의 dropLast()와 removeLast()의 차이점 정리
dropLast()
removeLast()
만일을 대비해서 총결산을 하기로 했다.두 가지 방법의 문서에 따라 총결하다.
dropLast(_:)
Returns a subsequence containing all but the specified number of final elements.
func dropLast(_ k: Int) -> Substring
끝에서 제k
개 원소를 제외한 부분의 집합을 반환값으로 되돌려줍니다.여기
dropLast()
에서는 객체 문자열에 지정된 꼬리 범위에서 제외된 나머지를 Substring
유형의 값으로 생성하고 반환합니다.따라서 대상 문자열(인스턴스)에서 값이 수정되지 않습니다.let str = "abcdef"
print(str.dropLast()) // abcde
print(str) // abcdef
removeLast(_:)
Removes and returns the last element of the collection.
@discardableResult mutating func removeLast() -> Character
collection의 마지막 요소를 배제하고 배제된 요소를 반환한다.(Removes 및 returns의 목적어는 항상 the last element)방법의 정의를 다시 한 번 봅시다.
@discardableResult
mutating
dropLast()
과 크게 다르다.var str2 = "abcdef" // mutating funcなので、letに対してremoveLast()は適用できない
print(str2.removeLast()) // f
print(str2) // abcde
var str3 = "abcdef"
str3.removeLast() // 返却値を演算に使わず、末尾要素の削除のみを行う
print(str3) // abcde
참고 자료
Reference
이 문제에 관하여(String의 dropLast()와 removeLast()의 차이점 정리), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://zenn.dev/mhackit/articles/306ad07d59af4985762f텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)