Seize the day
article thumbnail

1. Component

공식 문서:

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

<swift />
func components(separatedBy separator: String) -> [String]

1.1. 파라미터의 개수

- components 메서드의 경우는 매개변수가 separatedBy 하나만 있고 매개변수로 받은 separatedBy를 기준으로 문자열을 분리하게 된다.

1.2. return 타입

- 파라미터로 String을 받고 return 타입은 [String] 으로 반환한다.

1.3. Foundation 프레임 워크

- Foundation 프레임 워크에 속해있기 때문에 사용하기 위해서는 import 해야한다.

2. Split

공식 문서:

https://developer.apple.com/documentation/swift/string/split(separator:maxsplits:omittingemptysubsequences:)  

 

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

<swift />
func split( separator: Self.Element, maxSplits: Int = Int.max, omittingEmptySubsequences: Bool = true ) -> [Self.SubSequence]

2.1. 파라미터의 개수

- seperator, maxSplit, omittingEmptySubsequence 총 3가지가 있다.

2.1.1. seperator

- Character 타입으로 매개변수를 받아서 해당 인자를 기준으로 쪼개준다.

2.1.2. maxSplit

- Int 타입으로 매개변수를 받으며 의미는 쪼개는 횟수를 나타낸다.

- 0보다 크거나 같아야하며 기본값으로 Int.max 라고 나와있다.

<swift />
let str = "My name is Seonghyeon" var result = str.split(separator: " ", maxSplit: 1) print(result) // ["My","name is Seonghyeon"]

2.1.3. omittingEmptySubsequence

- omit이라는 단어는 생략을 의미한다. 따라서 빈 서브시퀀스를 생략할 수 있다는 옵션으로 이해하면 된다.

- Bool 타입으로 매개변수를 받게 된다.

- 기본값은 true로 설정되어 있으며, false와 함께 두 가지 경우로 나뉠 수 있다.

<swift />
var str = "My name is Seonghyeon " var result = str.split(separator: " ", omittingEmptySubsequenece: true) print(result) // -> ["My","name","is","Seonghyeon"] --------------------------------------------------------------------- var str1 = "My name is Seonghyeon " var result1 = str1.split(separator: " ", omittingEmptySubsequenece: false) print(result1) // -> ["My","name","is","Seonghyeon",""]

- 쉽게 설명해서 true일때는 빈 subsequence는 반환하지 않으며, false일때는 빈 subsequence까지 반환한다.

- 빈 subsequence가 여러개여도 모두 반환한다.

<swift />
var str = "My name is Seonghyeon " var result = str.split(separator: " ", omittingEmptySubsequences: false) print(result) //["My", "name", "is", "Seonghyeon", "", "", "", "", ""]

2.2. return 타입

- 파라미터로 Character 를 받고 return은 [Substring] 형태로 반환한다.

2.3. 표준 프레임 워크

split은 Swift 표준 라이브러리에 포함되어 있어 Foundation을 import 하지 않아도 사용할 수 있다.

 

✅ 시간 복잡도: O(n)

 

3. split VS components

결국 둘의 차이점은 다음과 같다.

 

1️⃣ 파라미터의 개수

- split는 3개(separator, maxSplit, omittingEmptySubsequeneces) , components는 1개(separatedBy)

 

2️⃣ return 타입

- split은 [Substring], components는 [String]

 

3️⃣ Foundation 프레임 워크 import 여부

- split은 Swift 표준 라이브러리, components는 Foundation 프레임 워크

 

4️⃣ 성능

split이 components에 비해서 성능이 좋다.(더 빠르다.)

<swift />
let str = "I am Seonghyeon " let splitedStr = str.split(separator: " ") print(splitedStr) // ["I", "am", "Seonghyeon"] let splitedStr2 = str.components(separatedBy: " ") print(splitedStr2) // ["I", "am", "Seonghyeon", "", "", "", "", "", "", "", "", ""]

3.0.1.  

- 상단의 예시처럼 성능적으로 차이는 구분자를 빈 문자열로 두고 문자열 내의 연속된 빈 공백 문자가 많을 때 차이가 난다.

- 따라서 문제를 풀 때 split을 사용하는게 더 효율적!

profile

Seize the day

@성 현

포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!