# Put letters from input in array

**URL:** <https://discourse.processing.org/t/put-letters-from-input-in-array/34486>\
**Category:** Coding Questions\
**Created:** [January 6, 2022, 11:57am UTC](https://discourse.processing.org/t/put-letters-from-input-in-array/34486 "2022-01-06T11:57:43Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![MoonAlien822](https://avatars.discourse-cdn.com/v4/letter/m/65b543/32.png) [@MoonAlien822](https://discourse.processing.org/u/MoonAlien822)\
**Post date:** [January 6, 2022, 11:57am UTC](https://discourse.processing.org/t/put-letters-from-input-in-array/34486/1 "2022-01-06T11:57:43Z")

</div>

How do I break “text” apart and put it into an array?

```auto
let input, button, greeting;

function setup() {
  // create canvas
  createCanvas(710, 400);

  input = createInput();
  input.position(20, 65);

  button = createButton('submit');
  button.position(input.x + input.width, 65);
  button.mousePressed(greet);

  greeting = createElement('h2', 'what do you want Rick to say?');
  greeting.position(20, 5);

  textAlign(CENTER);
  textSize(50);
}

function greet() {
  const text = input.value();
}

```

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [January 6, 2022, 1:38pm UTC](https://discourse.processing.org/t/put-letters-from-input-in-array/34486/2 "2022-01-06T13:38:20Z")

</div>

in a sense, the string is an array

use charAt to get the elements:

see last line

```auto

let input, button, greeting;

function setup() {
  // create canvas
  createCanvas(710, 400);

  input = createInput();
  input.position(20, 65);

  button = createButton('submit');
  button.position(input.x + input.width, 65);
  button.mousePressed(greet);

  greeting = createElement('h2', 'what do you want Rick to say?');
  greeting.position(20, 5);

  textAlign(CENTER);
  textSize(50);
}

function greet() {
  let text = input.value();
  console.log(text.charAt(3)); // !!!!
}

```

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [January 6, 2022, 3:55pm UTC](https://discourse.processing.org/t/put-letters-from-input-in-array/34486/3 "2022-01-06T15:55:34Z")

</div>

> [@MoonAlien822](#):
>
> How do I break “text” apart and put it into an array?

> [@Chrisir](#):
>
> In a sense, the string is an array.

Characters within a JS string can be individually read using the operator `[]` just like an array:

> **[String - JavaScript | MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String#character_access)**
>
> The String object is used to represent and manipulate a
> sequence of characters.

However if you prefer to convert it into an actual array you can invoke its String::**split()** function passing an empty string as its parameter:  
`const text = input.value();` → `const chars = input.value().split('');`

> **[String.prototype.split() - JavaScript | MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split)**
>
> The split() method of String values takes a pattern and divides this string into an ordered list of substrings by searching for the pattern, puts these substrings into an array, and returns the array.

For “word” splitting instead of an array of characters take a look at both **split()** & **splitTokens()**:

1. [reference | p5.js](http://p5js.org/reference/#/p5/split)
2. [reference | p5.js](http://p5js.org/reference/#/p5/splitTokens)
