Logic Machine Forum
Split a string into many string! - Printable Version

+- Logic Machine Forum (https://forum.logicmachine.net)
+-- Forum: LogicMachine eco-system (https://forum.logicmachine.net/forumdisplay.php?fid=1)
+--- Forum: Scripting (https://forum.logicmachine.net/forumdisplay.php?fid=8)
+--- Thread: Split a string into many string! (/showthread.php?tid=1121)



Split a string into many string! - phongvucba - 01.12.2017

Please help me, if anybody can do!
 I have a any string "abcd12345678901234567". Now I want to split the strings into two strings:
- The first string from the first letter to the 14th character. "abcd1234567890"
- The last string is from the 15th character to the end of the string. "1234567".
- Count the number of characters in the string "abcd12345678901234567". Because this string is always changing. so I need the last string to get the correct value ending.
Who can help me? thank you very much.


RE: Split a string into many string! - admin - 01.12.2017

Code:
str = 'abcd12345678901234567'
-- get length of a string
len = #str
-- get first 14 characters
a = string.sub(str, 1, 14)
-- get remaining characters
b = string.sub(str, 15)



RE: Split a string into many string! - phongvucba - 01.12.2017

Thank you very much admin!
I have a little question! please help me!
I have a string "Hold me tight or don't", but when displayed on the screen KNX it appears "Hold me tight or don't". Does KNX have no unicode code? and it transforms code(') into (' )
To be correct, please help me write a script:
-Search strings (' ) in string "Hold me tight or don't" and then change to (').
Thank so much!


RE: Split a string into many string! - Erwin van der Zwart - 01.12.2017

Hi,

Use this:
Code:
str = 'Hold me tight or don't'
str = string.gsub(str, ''', "'")
log(str) -- Hold me tight or don't

Other metadata options are in this function:

Code:
function unescapemetadata(str)
    str = string.gsub( str, '%%3a', ':' )
    str = string.gsub( str, '%%2f', '/' )
    str = string.gsub( str, '&lt;', '<' )
    str = string.gsub( str, '&gt;', '>' )
    str = string.gsub( str, '&quot;', '"' )
    str = string.gsub( str, '&apos;', "'" )
    str = string.gsub( str, '&#(%d+);', function(n) return string.char(n) end )
    str = string.gsub( str, '&amp;', '&' ) -- Be sure to do this after all others
    return str
end

BR,

Erwin


RE: Split a string into many string! - phongvucba - 02.12.2017

very good! I very thank friend! Smile