# Type Casting string to array

**URL:** <https://forums.swift.org/t/type-casting-string-to-array/73563>\
**Category:** Using Swift\
**Tags:** swift\
**Created:** [July 30, 2024, 2:56pm UTC](https://forums.swift.org/t/type-casting-string-to-array/73563 "2024-07-30T14:56:37Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![EngOmarElsayed](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/engomarelsayed/32/45556_2.png) [@EngOmarElsayed](https://forums.swift.org/u/EngOmarElsayed)\
**Post date:** [July 30, 2024, 2:56pm UTC](https://forums.swift.org/t/type-casting-string-to-array/73563/1 "2024-07-30T14:56:37Z")

</div>

What is the time complexity of type casting string to array and vice versa. is it O(n) or O(1) ? and why ?

---

<div class="post-metadata">

**Author:** ![scanon](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/scanon/32/2445_2.png) [@scanon](https://forums.swift.org/u/scanon)\
**Post date:** [July 30, 2024, 3:53pm UTC](https://forums.swift.org/t/type-casting-string-to-array/73563/2 "2024-07-30T15:53:42Z")

</div>

You can't cast a String to an Array. They are different types, with different memory layouts.

You can initialize a String from an Array, e.g.:

```swift
let array: [UInt8] = [72, 101, 108, 108, 111]
let string = String(validating: array, as: UTF8.self)! // "Hello"

```

and you can initialize an Array from a String or one of its views:

```swift
let arrayOfCharacters = Array(string) // Array<Character>
let arrayOfUTF8 = Array(string.utf8) // Array<UInt8>

```

both of these are O(n) operations, because they have to actually copy the data (and validate/transform it, in some cases).

If you want to do string-processing-type operations on buffers without copying the data, you will likely want to follow along with @Michael_Ilseman and @glessard's [proposal for string processing on contiguous storage](https://forums.swift.org/t/pitch-safe-utf-8-processing-over-contiguous-bytes/72742).

Another option which may or may not be viable for you today, depending on what you're doing, is to operate on the string's `.utf8` view directly, instead of on an `Array`. It is a collection of bytes, and you can do many of the things that you would do with an `Array` with that view directly. If you can provide some more context about what you're trying to do, we can offer some suggestions.

---

<div class="post-metadata">

**Author:** ![tera](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/tera/32/38201_2.png) [@tera](https://forums.swift.org/u/tera)\
**Post date:** [July 30, 2024, 8:07pm UTC](https://forums.swift.org/t/type-casting-string-to-array/73563/3 "2024-07-30T20:07:54Z")

</div>

I think you could get to the underlying bytes in `O(1)` if that's what you want doing. E.g. if the string in question is ascii then your bytes would be characters, otherwise it's probably UTF8 and the correspondence would be more complex.

---

<div class="post-metadata">

**Author:** ![EngOmarElsayed](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/engomarelsayed/32/45556_2.png) [@EngOmarElsayed](https://forums.swift.org/u/EngOmarElsayed)\
**Post date:** [July 31, 2024, 8:57am UTC](https://forums.swift.org/t/type-casting-string-to-array/73563/4 "2024-07-31T08:57:56Z")

</div>

@scanon So what made me ask this question is because of a problem I was solving on hackerRank, My solution exceeded the time limit of some of the tests. One part of the code I was taking an `Int` so instead of working with it I changed it like that `Array(String(10))` then after finishing I do the opposite. After searching I found a way to work with the `Int` directly and it worked and passed all the tests after removing `Array(String(10))` from code. This made me wonder what is the time complexity of this piece of code `Array(String(10))` 🤔 that made my code takes so long
