Try enclosing your code inside a 'script' tag of DSL language like the following piece of code:
pipeline {
agent any
stages {
stage('Test') {
steps {
script {
def string_var = "http://localhost:8081/artifactory/api/storage/myrepo/sub1/file.zip"
String[] arr= string_var.split('/');
println "${arr[0]}"
}
}
}
}
}
Executing code above I get this result on the console:
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Test)
[Pipeline] script
[Pipeline] {
[Pipeline] echo
http:
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Thus, giving the expected 'http:' String
Another Groovy way to get the string 'sub1' (regex):
String s = "http://localhost:8081/artifactory/api/storage/myrepo/sub1/file.zip"
def match = s =~ /(sub1)/
if (match.size() > 0) { // If url 's' contains the expected string 'sub1'
println match[0][1]
// Or do something with the match
}