Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

Javascript 

How to break up text from a string

So let's say I have

Code
var object = {
    "ip" : '',
    "port" : '',
    "user" : '',
    "pass" : ''
};

And when I save stuff, I save it as:
127.0.0.1:8080:user:pass

How can I enter it like the following, as I will be saving up to a few hundred of proxies, they'll all be on a seperate line (for instance:
127.0.0.1:8080:user:pass
127.0.0.1:8080:user:pass
127.0.0.1:8080:user:pass
127.0.0.1:8080:user:pass

This will enter 4 proxies, but then they must be displayed like this inside the object:

Code
object = {
    "ip" : '127.0.0.1',
    "port" : '8080',
    "user" : 'user',
    "pass" : 'pass'
};}

Thank you in advance.
I have never done anything in Node.JS, but I'm assuming you can use pure Javascript here?

If so, the split() method should be what you want.

Your code could look something like this:

Code
var proxy = "127.0.0.1:8080:user:pass";

var splitArray = proxy.split(":"); //Separates your string into an array of substrings, separated by ':'.

if(splitArray.length == 4) //To make sure all 4 needed object attributes are in the string.
{
   var ip = splitArray[0];
   var port = splitArray[1];
   var user = splitArray[2];
   var pass = splitArray[3];

   //Add these to your object now :)
   ...
}

I haven't tested the code, so there might be some syntax errors, or otherwise I did something wrong. The idea of a working code should be the same or very similar to this though.
(Apr 27, 2018 12:59 AM)V3t3r4n Wrote:  I have never done anything in Node.JS, but I'm assuming you can use pure Javascript here?

If so, the split() method should be what you want.

Your code could look something like this:

Code
var proxy = "127.0.0.1:8080:user:pass";

var splitArray = proxy.split(":"); //Separates your string into an array of substrings, separated by ':'.

if(splitArray.length == 4) //To make sure all 4 needed object attributes are in the string.
{
   var ip = splitArray[0];
   var port = splitArray[1];
   var user = splitArray[2];
   var pass = splitArray[3];

   //Add these to your object now :)
   ...
}

I haven't tested the code, so there might be some syntax errors, or otherwise I did something wrong. The idea of a working code should be the same or very similar to this though.

Split works perfectly, thank you!
I couldn't remember for the life of me what it was called lol.
(Apr 27, 2018 01:04 AM)v1zion Wrote:  Split works perfectly, thank you!
I couldn't remember for the life of me what it was called lol.

Awesome, glad I could help :)

I keep calling it 'explode' too, like PHP does for a similar function 😝


Possibly Related Threads...
Thread / Author Replies Views Last Post
[PHP] by TetraHCSep 4, 2019 03:12 AM 9 221 Sep 4, 2019 03:12 AM
Last Post: newQuery
[PHP] by the6ixMay 26, 2017 10:33 PM 0 90 May 26, 2017 10:33 PM
Last Post: the6ix
[PHP] by StreamahNov 19, 2016 08:56 PM 1 213 Nov 19, 2016 08:56 PM
Last Post: Mr.Steven