Example: string in string
Mình có 1 ý tưởng là mình tìm chuỗi a “lo” có nằm trong chuỗi b “Hello” không?
https://blogs.oracle.com/fadevrel/post/useful-groovy-string-functions
Introduction
Groovy Script is underpinned by Java within which there are classes and functions that are available for use within groovy. There are certain functions in the String java class that are useful when trying to process form field data. This blog post aims to go through some of those methods and explain how they can be used in the context of groovy and Application Composer.
Simple String functions
The String class comes with some standard functions that we can use to manipulate a string value input by an end user. Here is a list of them.
String Function | Sample | Description |
---|---|---|
contains | myStringVar.contains(<someOtherValue>) | Returns true if and only if this string contains the specified sequence of char values. |
equals | myStringVar.equals(<someOtherValue>) | This is similar to the above but has to be an exact match for the check to return a true value |
endsWith | myStringVar.endsWith(<suffix>) | This method checks the new value contains an ending string |
startsWith | myStringVar.startsWith(<prefix>) | This method checks the new value contains an starting string |
equalsIgnoreCase | myStringVar.equalsIgnoreCase(<someOtherValue>) | The same as equals but without case sensitivity |
isEmpty | myStringVar.isEmpty() | Checks if myStringVar is populated or not. |
matches | myStringVar.matches(<someOtherValue>) | this is the same as equals with the slight difference being that matches takes a regular string as a parameter unlike equals which takes another String object |
replace | myStringVar.replace(<oldVal>,<newVal>) | Returns a string resulting from replacing all occurrences of oldChar in this string with newChar |
replaceAll | myStringVar.replaceAll(<oldValRegex>,<newVal>) | Replaces each substring of this string that matches the given regular expression with the given replacement |
split | myStringVar.split(<regexPattern>) | Splits this string around matches of the given regular expression |
During field validation the variable newValue
is the parameter name used to retrieve the field value after a form is submitted. Similarly oldValue
is the previously stored value during field validation. Either or both of these variableNames would be used instead of myStringVar
when performing field validation.
Further Information
Here are some related links…
- String Java Class javadoc
- Regular Expression javadoc
- Blog post on Regular Expression Examples
- Common Regular Expression Patterns blog post
Để hiểu hơn func đó chạy sao:
Mình sẽ lên google search



Note:
Có vấn đề mà mình gặp là:
The split() method in Java does not work on a dot (.)
java.lang.String.split
splits on regular expressions, and .
in a regular expression means “any character”.
Try temp.split("\\.")
.

class Example {
static void main(String[] args) {
String a = "jenkins-validate-grpc.dev.tiki.services";
String[] str;
str = a.split('\\.');
for( String values : str )
println(values);
println(str[0])
}
}
