Super Contributor.. Giulio Soleni Super Contributor..
Super Contributor..
2555 views

split a string into pieces of N characters

Hi,

I need to split an input sting into pieces of N chars.

Since I did not find any predefined operation to accomplish this task I thought I could use a simple "No Operation" step and arrange the results with a sleep script, but I cannot find any example to start.

 

The perl code should be pretty simple... something like the following:

 

$string = $inString; # inString should be the 1st step input
$partLength = $inPart; # inPart should be the 2nd step input
my @parts = $string =~ /.{1,$partLength}/sg;
print @parts;

Anyone may help me in setting up a Sleep or Rhino scriplet to define this step?

 

Or maybe there's a better way in OO to manage strings and split a long string in several pieces of fixed lenght.

 

Thank you,

Giulio

Labels (1)
0 Likes
4 Replies
Absent Member.. ntt00015 Absent Member..
Absent Member..

Re: split a string into pieces of N characters

Hi,

Try this one. Hope this helps.

 

Try to write scriptlet. Just add a "Do Nothing" operation to your flow.
3 inputs : 1. sourceString, 2.delimiter, 3. substringLength
2 outputs : 1. substringList, 2. listCount

*******
//Scriptlet
myString= scriptletContext.get("sourceString"); //input 1
myDelimiter=scriptletContext.get("delimiter"); //input 2
mySubStrLen=scriptletContext.get("substringLength"); //input 3

mySubStrArray=new Array();
forCount = Math.ceil( myString.length / mySubStrLen);
for(i=0;i<forCount;i++)
{
arrPosStart=i*mySubStrLen;
arrPosEnd=(i+1)*mySubStrLen;
subDummy = myString.slice(arrPosStart,arrPosEnd);
mySubStrArray[i]= subDummy;

}

myList=mySubStrArray.join(myDelimiter);

scriptletContext.put("substringList",myList); //output 1
scriptletContext.put("listCount",forCount); //output 2

*******
After that you can use a "List Item Grabber" operation in a loop.
input parameters:
list<= substringList (output of no operation)
delimiter<= delimiter (input of no operation)
index<= the number of cell in the array (from loop operation result)

0 Likes
Absent Member.. koby_el Absent Member..
Absent Member..

Re: split a string into pieces of N characters

You can play with the result of a "Display" operation, or a "Do Nothing" operation. (I did it with that)

But you'll have to relay on some characters or something like that....

0 Likes
meetlesli Absent Member.
Absent Member.

Re: split a string into pieces of N characters

is there any library for string split

Thanks,
Leslie
0 Likes
Absent Member.. Steve_Drummond Absent Member..
Absent Member..

Re: split a string into pieces of N characters

Is there a HPOO Operation that splits a string? No.

 

Is there a library for String split? Yes.

 

str.split([separator][, limit])

separator
Specifies the character(s) to use for separating the string. The separator is treated as a string or a regular expression. If separator is omitted, the array returned contains one element consisting of the entire string. If separator is an empty string, str is converted to an array of characters.
limit
Integer specifying a limit on the number of splits to be found. The split method still splits on every match of separator, but it truncates the returned array to at most limit elements.

 From https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split

0 Likes
The opinions expressed above are the personal opinions of the authors, not of Micro Focus. By using this site, you accept the Terms of Use and Rules of Participation. Certain versions of content ("Material") accessible here may contain branding from Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017, the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE marks are the property of their respective owners.