Forums

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

How to parse ArrayList to String, without [] bracket?

Teja
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
June 6, 2019

Hi,

I wanted to compare my unreleased version with user selected version picker value.

but the problem is when I use this code  

def releaseVerField = ComponentAccessor.customFieldManager.getCustomFieldObjectByName("Target Version")
def releaseVerFieldValue = issue.getCustomFieldValue(releaseVerField)

it retrieves [19.2.8] with bracket, how can cast this List to String.

I am in a real need of help, can anyone with this below code.

Since I am new to groovy so, 

import com.atlassian.jira.ComponentManager
import com.atlassian.jira.project.version.VersionManager
import com.atlassian.jira.component.ComponentAccessor
import com.opensymphony.workflow.InvalidInputException;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.project.ProjectImpl

ComponentManager componentManager = ComponentManager.getInstance()

VersionManager versionManager = ComponentAccessor.getVersionManager();
def releaseVerField = ComponentAccessor.customFieldManager.getCustomFieldObjectByName("Target Version")
def releaseVerFieldValue = issue.getCustomFieldValue(releaseVerField)
log.warn(releaseVerFieldValue)

List<VersionManager> versions=versionManager.getVersionsUnreleased(10207,true) as List
def tarversion= versions.iterator().next().toString()

if (releaseVerFieldValue !=tarversion)
{
log.warn(versions.iterator().next())
return true

}
else
throw newInvalidInputException("Target version must be unreleased");

Output:testfield.PNG

Thanks & Regards

1 answer

1 accepted

1 vote
Answer accepted
Payne
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
June 6, 2019

You can assign your List to a String, and the necessary casting will be performed. You can then use a function that I wrote for this exact purpose to strip the brackets.

e.g.

String releaseVerFieldValueStr = releaseVerFieldValue
releaseVerFieldValueStr = stripBrackets(releaseVerFieldValueStr)

def stripBrackets(String str){
String retStr = str
if(str == null || str.length() < 2)
{
return str
}
if(retStr.substring(0,1).equals("[")){
retStr = retStr.substring(1)
}
if(retStr.substring(retStr.length()-1).equals("]")){
retStr = retStr.substring(0,retStr.length()-1)
}
return retStr
}
Teja
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
June 9, 2019

Hi @Payne Great, it worked. Well done.

Regards

Tejas

Teja
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
June 28, 2019

Hi @Payne How can we write this groovy code to Java code?

Suggest an answer

Log in or Sign up to answer