# \[p5.js\] Split operation does not work with text obtained by loadStrings

**URL:** https://discourse.processing.org/t/p5-js-split-operation-does-not-work-with-text-obtained-by-loadstrings/34056
**Category:** Beginners
**Created:** [December 11, 2021, 3:15pm UTC](https://discourse.processing.org/t/p5-js-split-operation-does-not-work-with-text-obtained-by-loadstrings/34056 "2021-12-11T15:15:37Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![GWAK](https://avatars.discourse-cdn.com/v4/letter/g/13edae/32.png) [@GWAK](https://discourse.processing.org/u/GWAK)
#### Post date: [December 11, 2021, 3:15pm UTC](https://discourse.processing.org/t/p5-js-split-operation-does-not-work-with-text-obtained-by-loadstrings/34056/1 "2021-12-11T15:15:37Z")

</div>

**[P5.JS]**

```auto
var value1;
var value2;

function setup() { 

    createCanvas(450, 150); 
    background(220); textSize(12); 
      
     let url = " SITE IP ADDRESS /split_test/user_check.php?";
     value1 = loadStrings(url);
     console.log(value1);
     
     value2 = split(value1, '_');  
     console.log(value2);
    
} 

function draw() { 
    
    text("Splitted string is: " + value1, 50, 15); 
    text("Splitted string is: " + value2, 50, 30);    

    noloop();
    
}

```

Split operation does not work with text obtained by loadStrings .

1. Get text value via loadString.
2. We are going to do classification through the ‘split’ function.
3. I get an error in split.
4. What is the reason?

---

<div class="post-metadata">

### Author: ![GWAK](https://avatars.discourse-cdn.com/v4/letter/g/13edae/32.png) [@GWAK](https://discourse.processing.org/u/GWAK)
#### Post date: [December 11, 2021, 3:30pm UTC](https://discourse.processing.org/t/p5-js-split-operation-does-not-work-with-text-obtained-by-loadstrings/34056/2 "2021-12-11T15:30:13Z")

</div>

```auto
var value1;
var value2;
let value3;

function setup() { 

    createCanvas(450, 150); 
    background(220); textSize(12); 
      
     let url = " SITE IP ADDRESS /split_test/user_check.php?";
     value1 = loadStrings(url);
     console.log(value1);    
} 

function draw() { 
    value3 = value1.toString();
    value2 = split(value3, "_");  

    text("string is: " + value1, 50, 15); 
    text("Splitted string is: " + value2[0], 50, 30);    
    text("Splitted string is: " + value2[1], 50, 45);  
    text("Splitted string is: " + value2[2], 50, 60);  

    noloop();    
}

```

I solved it. I don’t know exactly why, but here’s my conclusion:

1. You need to convert it to toString().
2. ‘split’ function does not work in ‘setup’.

I don’t know the exact reason, but the conclusion is the same as 1-2.

---

<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: [December 11, 2021, 3:42pm UTC](https://discourse.processing.org/t/p5-js-split-operation-does-not-work-with-text-obtained-by-loadstrings/34056/3 "2021-12-11T15:42:11Z")

</div>

- Function [**loadStrings()**](https://p5js.org/reference/#/p5/loadStrings) returns an array of strings.
- Function [**split()**](https://p5js.org/reference/#/p5/split) expects 1 string and then returns an array of strings as well.
- So you can’t pass an array of strings to function [**split()**](https://p5js.org/reference/#/p5/split).
- BtW you can use [**join()**](https://p5js.org/reference/#/p5/join) to convert an array of strings to 1 string.

---

<div class="post-metadata">

### Author: ![GWAK](https://avatars.discourse-cdn.com/v4/letter/g/13edae/32.png) [@GWAK](https://discourse.processing.org/u/GWAK)
#### Post date: [December 11, 2021, 4:13pm UTC](https://discourse.processing.org/t/p5-js-split-operation-does-not-work-with-text-obtained-by-loadstrings/34056/4 "2021-12-11T16:13:08Z")

</div>

@GoToLoop

Dear GoToLoop,

It is established in general ‘JAVA Mode’.  
And in the general case, ‘P5.JS’ is established.  
I understand 100% of what you are trying to explain.

However, it seems that an error occurs when data is fetched through http.

1. within php to fetch data,  
`header("Access-Control-Allow-Origin: *");`  
should declare

2. When importing data,  
It must be put in draw(), but it is not executed in setup().  
I can’t quite understand

---

<div class="post-metadata">

### Author: ![GWAK](https://avatars.discourse-cdn.com/v4/letter/g/13edae/32.png) [@GWAK](https://discourse.processing.org/u/GWAK)
#### Post date: [December 11, 2021, 4:31pm UTC](https://discourse.processing.org/t/p5-js-split-operation-does-not-work-with-text-obtained-by-loadstrings/34056/5 "2021-12-11T16:31:49Z")

</div>

![fig](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/f/fe237d27581bdd421e10c2b89d9a32f315b0b8ef.jpeg)

This is my inference.

Some P5.JS operates as a ‘thread’ even in the same function.  
Due to the time difference, there is a form in which an error occurs.

to solve this

> function DB\_CHECK() { if(DB\_EN){ if(TIMER\_DB \< millis()){
> 
> }}

1. First, after executing ‘loadStrings()’

2. Give a delay of 100ms

3. Implementing split, the error disappeared.

---

<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: [December 11, 2021, 4:38pm UTC](https://discourse.processing.org/t/p5-js-split-operation-does-not-work-with-text-obtained-by-loadstrings/34056/6 "2021-12-11T16:38:53Z")

</div>

The correct place for loading assets is inside **preload()**:  
[p5js.org/reference/#/p5/preload](http://p5js.org/reference/#/p5/preload)

**loadStrings()**'s 1st example uses **preload()** btW:  
[p5js.org/reference/#/p5/loadStrings](http://p5js.org/reference/#/p5/loadStrings)

---

<div class="post-metadata">

### Author: ![GWAK](https://avatars.discourse-cdn.com/v4/letter/g/13edae/32.png) [@GWAK](https://discourse.processing.org/u/GWAK)
#### Post date: [December 11, 2021, 5:51pm UTC](https://discourse.processing.org/t/p5-js-split-operation-does-not-work-with-text-obtained-by-loadstrings/34056/7 "2021-12-11T17:51:30Z")

</div>

[![](https://img.youtube.com/vi/DIFEWOY0gDI/maxresdefault.jpg "WEB을 통한 js코딩 연습하기") ](https://www.youtube.com/watch?v=DIFEWOY0gDI)

Dear GoToLoop,

you’re right. However, problems arise when you need to receive DB data or data regularly.  
So, you have to think about the appropriate delay and implement it.

---

<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: [December 11, 2021, 5:58pm UTC](https://discourse.processing.org/t/p5-js-split-operation-does-not-work-with-text-obtained-by-loadstrings/34056/8 "2021-12-11T17:58:42Z")

</div>

> [@GWAK](#):
>
> So, you have to think about the appropriate delay and implement it.

Outside of **preload()** we need to pass a callback function as the 2nd argument, so it runs when the loading is successful.

---

<div class="post-metadata">

### Author: ![GWAK](https://avatars.discourse-cdn.com/v4/letter/g/13edae/32.png) [@GWAK](https://discourse.processing.org/u/GWAK)
#### Post date: [December 12, 2021, 11:45pm UTC](https://discourse.processing.org/t/p5-js-split-operation-does-not-work-with-text-obtained-by-loadstrings/34056/9 "2021-12-12T23:45:46Z")

</div>

@GoToLoop

Thank you for answer.

The ‘preload()’ function knows what to do in advance.

However, what I want to say is

1. Need to use ‘loadStrings’ periodically

2. (Because it is not used only once,) ‘preload’ cannot be the solution.

3. Therefore, it should proceed through time difference calculation through millis().

---

<div class="post-metadata">

### Author: ![GWAK](https://avatars.discourse-cdn.com/v4/letter/g/13edae/32.png) [@GWAK](https://discourse.processing.org/u/GWAK)
#### Post date: [December 13, 2021, 5:24am UTC](https://discourse.processing.org/t/p5-js-split-operation-does-not-work-with-text-obtained-by-loadstrings/34056/10 "2021-12-13T05:24:51Z")

</div>

@GoToLoop

```auto
  loadStrings(url, function(response){

  });

```

It is easily solved by using [callback].  
thank you.
