Aurora_Gaimon

CA PPM: How to "return part of a string" in Gel

Blog Post created by Aurora_Gaimon Employee on Mar 23, 2017

Gel can be easy or very difficult depending on the available documentation. Many of us, we learn by seeing examples or just by experimenting. 

 

Today I'd like to explain how to return part of a string in GEL script. I'm pretty sure other peers may have many other ways and valid example, but this is how I do it, using substring()  java method:

 

substring(int startIndex, int endIndex)

startIndex : starting index is inclusive

endIndex : ending index is exclusive

or/and:
substring(int startIndex)

startIndex : starting index is inclusive


Let's take string '12345' for the following examples.

Sample 1: Return FIRST digit

<gel:script xmlns:core="jelly:core" xmlns:gel="jelly:com.niku.union.gel.GELTagLibrary" xmlns:util="jelly:util" >

<core:set var="digitString">12345</core:set>
<core:set var="firstDigit" value="${digitString.substring(0,1)}"/>
<gel:log>${firstDigit}</gel:log>


<core:if test="${firstDigit == '1'}">
<gel:log> it's 1 </gel:log>
</core:if>

<core:if test="${firstDigit != '1'}">
<gel:log> it's NOT 1 </gel:log>
</core:if>

</gel:script>

 

Results:

 

 

Sample 2: Return SECOND digit
<gel:script xmlns:core="jelly:core" xmlns:gel="jelly:com.niku.union.gel.GELTagLibrary" xmlns:util="jelly:util" >

<core:set var="digitString">12345</core:set>
<core:set var="secondDigit" value="${digitString.substring(1,2)}"/>
<gel:log>${secondDigit}</gel:log>


<core:if test="${secondDigit == '2'}">
<gel:log> it's 2 </gel:log>
</core:if>

<core:if test="${secondDigit != '2'}">
<gel:log> it's NOT 2 </gel:log>
</core:if>

</gel:script>

 

Results:

 

Sample 3: Return SECOND and THIRD digit
<gel:script xmlns:core="jelly:core" xmlns:gel="jelly:com.niku.union.gel.GELTagLibrary" xmlns:util="jelly:util" >

<core:set var="digitString">12345</core:set>
<core:set var="digit_2_and_3" value="${digitString.substring(1,3)}"/>
<gel:log>${digit_2_and_3}</gel:log>


<core:if test="${digit_2_and_3 == '23'}">
<gel:log> it's 23 </gel:log>
</core:if>

<core:if test="${digit_2_and_3 != '23'}">
<gel:log> it's NOT 23 </gel:log>
</core:if>

</gel:script>

 

Results:

 

 

And so on....

 

Sample 4: All string FROM SECOND digit

<gel:script xmlns:core="jelly:core" xmlns:gel="jelly:com.niku.union.gel.GELTagLibrary"  xmlns:util="jelly:util" >
    
<core:set var="digitString">12345</core:set>
<core:set var="noFirstDigit" value="${digitString.substring(1)}"/>
<gel:log>${noFirstDigit}</gel:log>


<core:if test="${noFirstDigit == '2345'}">
<gel:log> it's 2345 </gel:log>
</core:if>

<core:if test="${noFirstDigit != '2345'}">
<gel:log> it's NOT 2345 </gel:log>
</core:if>
    
</gel:script>

Results:

 

 

Reference:

Java String substring() method - javatpoint 

 

If you need help with GEL scripting, engage CA Services or ask questions in CA Communities.

 

That’s all. Thanks for reading until here. Did you like it? Please, don’t be shy and share it.

Outcomes