cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - Learn all about the Community Ranking System, a fun gamification element of the PTC Community. X

Can I divide a string into several substrings and save them into a database?

dawnman
2-Explorer

Can I divide a string into several substrings and save them into a database?

I need to get a 2D matrix barcode data from a PLC and split them into date, time, part number, cavity number, etc. and then save them into the database. Is it possible to do so?

ACCEPTED SOLUTION

Accepted Solutions
Velkumar
19-Tanzanite
(To:dawnman)

Hi @dawnman 

 

Please use the below code to split values from a string

var sampleString = 'EBABC23192124466BB68 10C700 AC';
var firstValue = sampleString.substring(0,5);
var secondValue = parseInt(sampleString.substring(5,10));
var thirdValue = parseInt(sampleString.substring(10,11));
var fourthValue = parseInt(sampleString.substring(11,12));
var fifthValue = parseInt(sampleString.substring(12,16));
var sixthValue= sampleString.substring(16);

 

I would recommend changing the PLC output. While constructing a string use delimiter/separator to separate each data.

 

For example: In Sample Value - 'EBABC-23192-1-2-4466-BB68 10C700 AC' each value is separated by the symbol '-'. It will avoid parsing/splitting errors when each value length grows in the future and it is simpler to split values in code

 

Something like this :

var sampleString = 'EBABC-23192-1-2-4466-BB68 10C700 AC';
var valueArray = sampleString.split("-");
var result = valueArray;

 

Velkumar_0-1702352217179.png

 

/VR

 

 

 

 

View solution in original post

3 REPLIES 3
Velkumar
19-Tanzanite
(To:dawnman)

Hi @dawnman 

 

Yes, you can split the string and write back to DB.

 

Could you please post a sample format 

 

/VR

dawnman
2-Explorer
(To:Velkumar)

A string that is similar to "EBABC23192000466BB68 10C700 AC" will be received. Then, I need to separate it into substrings as below and save them.
EBABC (string)
23192 (numeric)
0 (numeric)
0 (numeric)
466(0466, numeric)
BB68 10C700 AC (string)

Velkumar
19-Tanzanite
(To:dawnman)

Hi @dawnman 

 

Please use the below code to split values from a string

var sampleString = 'EBABC23192124466BB68 10C700 AC';
var firstValue = sampleString.substring(0,5);
var secondValue = parseInt(sampleString.substring(5,10));
var thirdValue = parseInt(sampleString.substring(10,11));
var fourthValue = parseInt(sampleString.substring(11,12));
var fifthValue = parseInt(sampleString.substring(12,16));
var sixthValue= sampleString.substring(16);

 

I would recommend changing the PLC output. While constructing a string use delimiter/separator to separate each data.

 

For example: In Sample Value - 'EBABC-23192-1-2-4466-BB68 10C700 AC' each value is separated by the symbol '-'. It will avoid parsing/splitting errors when each value length grows in the future and it is simpler to split values in code

 

Something like this :

var sampleString = 'EBABC-23192-1-2-4466-BB68 10C700 AC';
var valueArray = sampleString.split("-");
var result = valueArray;

 

Velkumar_0-1702352217179.png

 

/VR

 

 

 

 

Announcements


Top Tags