오늘은 문자열을 어떤 문자를 기준으로 끊을 때 사용하는
components(separatedBy: ) 와 split(separator: )의 차이에 대해
알아보려고 한다.
> components(separatedBy: ) 와 split(separator: ) 공식 문서 보러 가기
https://developer.apple.com/documentation/foundation/nsstring/1413214-components
components(separatedBy:) | Apple Developer Documentation
Returns an array containing substrings from the receiver that have been divided by a given separator.
developer.apple.com
https://developer.apple.com/documentation/foundation/nsstring/1410120-components
components(separatedBy:) | Apple Developer Documentation
Returns an array containing substrings from the receiver that have been divided by characters in a given set.
developer.apple.com
split(separator:maxSplits:omittingEmptySubsequences:) | Apple Developer Documentation
Returns the longest possible subsequences of the collection, in order, around elements equal to the given element.
developer.apple.com
📌 components
우선 components를 살펴보자.
components는 NSString의 인스턴스 메서드로
separator로 CharaterSet 혹은 String 타입을 받을 수 있다.
그리고 반환값은 String 배열 타입이다.
파라미터로 separator에
나누고 싶은 기준 값을 넘겨준다.
let string = "hi this is me joy"
let componetsArray = string.components(separatedBy: " ")
print(componetsArray)
// ["hi", "this", "is", "me", "", "", "", "", "", "", "joy"]
이때 쪼갠 결과에 빈 문자열도 포함하여 반환해준다.
📌 split
그 다음으론 split을 살펴보자.
split은 String의 인스턴스 메서드로
separator, maxSplits, omittingEmptySubsequences 의 매개변수를 받을 수 있다.
그리고 반환값은 String.SubSequence 배열 타입이다.
그럼 split의 파라미터를 살펴보자.
-
우선 separator는 Character 타입으로
나누고 싶은 기준 값을 넘겨준다.
-
maxSplits는 Int 타입으로
분할할 횟수를 나타낸다.
default 값으로 Int.max가 지정되어 있어
필요하지 않다면 해당 파라미터는 넘겨주지 않아도 된다.
반환 값은 maxSplits번 분할한
maxSplits+1개의 count을 가진 배열이 반환된다.
-
omittingEmptySubsequences는 Bool 타입으로
빈 시퀀스를 제외할지의 여부를 나타낸다.
default 값으로 true가 지정되어 있어
필요하지 않다면 해당 파라미터는 넘겨주지 않아도 된다.
omittingEmptySubsequences가 true로 되어있다면
분할한 결과 값에 빈 시퀀스를 제외한다.
let string = "hi this is me joy"
let splitArray = string.split(separator: " ")
let splitArray2 = string.split(separator: " ", maxSplits: 2)
let splitArray3 = string.split(separator: " ", omittingEmptySubsequences: false)
print(splitArray)
print(splitArray2)
print(splitArray3)
// ["hi", "this", "is", "me", "joy"]
// ["hi", "this", "is me joy"]
// ["hi", "this", "is", "me", "", "", "", "", "", "", "joy"]
기본적으로 separator 값만 넘겨주었을 때에는
separator를 기준으로 분할한 값에 빈 시퀀스를 제외한 값을 반환한다.
(ex. splitArray)
만일 maxSplits 값을 넘겨주었다면
maxSplits번 분할되어 maxSplits + 1의 길이만큼의 배열이 반환된다.
(ex. splitArray2)
omittingEmptySubsequences을 false로 한다면
componets 결과값과 마찬가지로 빈 시퀀스까지 포함하여 결과값을 반환한다.
(ex. splitArray3)
📌 components vs. split 정리
components(separatedBy: ) | split(separator: ) | |
결과값 타입 | [String] | [Substring] |
파라미터 | separator: String // 분할하려는 구분 값 | - separator: Character // 분할하려는 구분 값 - maxSplits: Int // 분할 횟수 (default: Int.max) - omittingEmptySubsequences: Bool // 빈 시퀀스 제외 여부 (deafult: true) |
차이점 | components는 분할 결과에 빈 문자열이 포함되어 있다면 빈 문자열도 반환되지만, split은 기본적으로 분할 결과값에 빈 시퀀스를 제외하여 반환해준다. |
|
성능 | split은 기본적으로 빈 시퀀스를 제외하고 결과를 반환하지만 components는 빈 문자열도 포함하여 결과를 반환하기 때문에 분할하려는 문자열에 빈 공간이 많다면 components가 성능적으로 느리다고 한다. |
'TIL' 카테고리의 다른 글
[TIL] Swift - for문 stride (through vs. to) (0) | 2024.08.16 |
---|---|
[TIL] SwiftUI - Extra argument in call 오류 (0) | 2023.04.18 |