# String To Array

**URL:** <https://forum.dfinity.org/t/string-to-array/14938>\
**Category:** Developers\
**Tags:** Functional-Programming\
**Created:** [August 17, 2022, 1:53pm UTC](https://forum.dfinity.org/t/string-to-array/14938 "2022-08-17T13:53:27Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![krunalshoppiko](https://avatars.discourse-cdn.com/v4/letter/k/ad7895/32.png) [@krunalshoppiko](https://forum.dfinity.org/u/krunalshoppiko)\
**Post date:** [August 17, 2022, 1:53pm UTC](https://forum.dfinity.org/t/string-to-array/14938/1 "2022-08-17T13:53:27Z")

</div>

I used JSON.stringify() to store text in backend. i get the result like this “[10,11,12,9]” but i want an array like [10,11,12,9] to use in for loop . how to remove “” form this. i try the Text.trimStart and Text.split(obj, #char ‘/"’) but it is not working. how do i solve this problem.

---

<div class="post-metadata">

**Author:** ![timo](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/timo/32/5543_2.png) [@timo](https://forum.dfinity.org/u/timo)\
**Post date:** [August 17, 2022, 6:32pm UTC](https://forum.dfinity.org/t/string-to-array/14938/2 "2022-08-17T18:32:13Z")

</div>

I would say `Text.tokens` should do the trick if you call it with a pattern that matches any of these four characters: " ,  
See: [Text | Internet Computer Home](https://internetcomputer.org/docs/current/references/motoko-ref/text/)  
That gives you an iterator that you can use in your loop.

You may still have to convert the text 10, 11, etc to type Nat if that’s your application.

---

<div class="post-metadata">

**Author:** ![krunalshoppiko](https://avatars.discourse-cdn.com/v4/letter/k/ad7895/32.png) [@krunalshoppiko](https://forum.dfinity.org/u/krunalshoppiko)\
**Post date:** [August 17, 2022, 6:54pm UTC](https://forum.dfinity.org/t/string-to-array/14938/3 "2022-08-17T18:54:34Z")

</div>

But how to make it dynamic. can you provide some example using “[10,11,12,9]” so i can make for loop using this array and get the data for each key.

---

<div class="post-metadata">

**Author:** ![paulyoung](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/paulyoung/32/5251_2.png) [@paulyoung](https://forum.dfinity.org/u/paulyoung)\
**Post date:** [August 17, 2022, 7:59pm UTC](https://forum.dfinity.org/t/string-to-array/14938/4 "2022-08-17T19:59:48Z")

</div>

I’m not sure if this needs updating but it might be a good place to start:

> **[GitHub - kritzcreek/motoko-json](https://github.com/kritzcreek/motoko-json)**
>
> Contribute to kritzcreek/motoko-json development by creating an account on GitHub.

---

<div class="post-metadata">

**Author:** ![timo](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/timo/32/5543_2.png) [@timo](https://forum.dfinity.org/u/timo)\
**Post date:** [August 17, 2022, 9:31pm UTC](https://forum.dfinity.org/t/string-to-array/14938/5 "2022-08-17T21:31:26Z")

</div>

```auto
import Text "mo:base/Text";
import Iter "mo:base/Iter";

actor {
  let t : Text = "\"[10,11,12,9]\"";

  public func f() : async [Text] {
    var s = t;
    s := Text.trimStart(s, #text "\"[");
    s := Text.trimEnd(s, #text "]\"");
    let i = Text.tokens(s, #char ',');
    Iter.toArray<Text>(i);
  };
}

```

It’s not very robust though. Additional spaces can break it. You’d have to trim them, too.

---

<div class="post-metadata">

**Author:** ![krunalshoppiko](https://avatars.discourse-cdn.com/v4/letter/k/ad7895/32.png) [@krunalshoppiko](https://forum.dfinity.org/u/krunalshoppiko)\
**Post date:** [August 18, 2022, 8:56am UTC](https://forum.dfinity.org/t/string-to-array/14938/6 "2022-08-18T08:56:27Z")

</div>

Thanks paulyoung.  
this motoko-json needs improvement.

---

<div class="post-metadata">

**Author:** ![krunalshoppiko](https://avatars.discourse-cdn.com/v4/letter/k/ad7895/32.png) [@krunalshoppiko](https://forum.dfinity.org/u/krunalshoppiko)\
**Post date:** [August 18, 2022, 8:58am UTC](https://forum.dfinity.org/t/string-to-array/14938/7 "2022-08-18T08:58:54Z")

</div>

it’s give me this kind of error.  
base/Text.mo:259.15-259.20: internal error.
