当前位置:网站首页>[groovy] string (string injection function | asBoolean | execute | minus)
[groovy] string (string injection function | asBoolean | execute | minus)
2022-07-05 03:12:00 【Programmer community】
List of articles
- One 、 String injection function
- 1、 String to Boolean asBoolean function
- 2、 Execute the command corresponding to the string execute function
- 3、 String subtraction minus function
- Two 、 Complete code example
One 、 String injection function
Groovy by character string class , Injected a series of methods , Here are some important injection methods ; by Groovy String extension function , It's all defined in
public class StringGroovyMethods extends DefaultGroovyMethodsSupportClass ;
1、 String to Boolean asBoolean function
A function that converts a string to a Boolean value :
- String length is 0 , Then return to false ;
- String length greater than 0 , return true ;
"".asBoolean();Injected asBoolean function :
public class StringGroovyMethods extends DefaultGroovyMethodsSupport {
/** * The string (CharSequence Example ) Force to Boolean . * If the length of the string is 0, The string is forced to false, * vice versa . * * @param String specifies the character sequence * @ Returns a Boolean value * @ from 1.7 Start .0 */ public static boolean asBoolean(CharSequence string) {
return string.length() > 0; }}Execute the following code :
class Test {
static void main(args) {
println "".asBoolean(); }}Execution results :
false2、 Execute the command corresponding to the string execute function
Call string execute() Method , This can be performed String command , The premise is that the string must be a system command , Can't be any string ;
Inject execute() function :
public class StringGroovyMethods extends DefaultGroovyMethodsSupport {
/** * take <code>self</code> The specified command is executed as a command line process . * <p> More control over process construction , You can use * <code>java.lang.ProcessBuilder</code>. * * @param self Command line string * @ Returning this command line indicates the process just started * @ In the event of a IOException Throw when IOException. * @ since 1.0 since */ public static Process execute(final String self) throws IOException {
return Runtime.getRuntime().exec(self); }}Execute the following code :
class Test {
static void main(args) {
println "cmd /c groovy -v".execute().text }}Execution results :
Groovy Version: 3.0.9 JVM: 1.8.0_91 Vendor: Oracle Corporation OS: Windows 103、 String subtraction minus function
Subtract between two strings , Equivalent to from a large string , Delete the subtracted small string ;
Inject minus() function :
public class StringGroovyMethods extends DefaultGroovyMethodsSupport {
/** * Remove part of the string . This will replace the first event * The goal is . take self Medium toString() And “” matching , And return the result . * * @param self character string * @param target Object representing the part to be removed * @ Returns a string minus the part to be deleted * @ since 1.0 since */ public static String minus(String self, Object target) {
String text = DefaultGroovyMethods.toString(target); int index = self.indexOf(text); if (index == -1) return self; int end = index + text.length(); if (self.length() > end) {
return self.substring(0, index) + self.substring(end); } return self.substring(0, index); }}Code example :
class Test {
static void main(args) {
println "HelloWorld" - "World" }}Execution results :
HelloTwo 、 Complete code example
Complete code example :
class Test {
static void main(args) {
println "".asBoolean(); println "cmd /c groovy -v".execute().text println "HelloWorld" - "World" }}Execution results :
falseGroovy Version: 3.0.9 JVM: 1.8.0_91 Vendor: Oracle Corporation OS: Windows 10Hello
边栏推荐
- The database and recharge are gone
- About MySQL database connection exceptions
- Azkaban overview
- [2022 repair version] community scanning code into group activity code to drain the complete operation source code / connect the contract free payment interface / promote the normal binding of subordi
- Returns the lowest common ancestor of two nodes in a binary tree
- Machine learning experiment report 1 - linear model, decision tree, neural network part
- 問下,這個ADB mysql支持sqlserver嗎?
- Talk about the SQL server version of DTM sub transaction barrier function
- Single box check box
- Voice chip wt2003h4 B008 single chip to realize the quick design of intelligent doorbell scheme
猜你喜欢
![ASP. Net core 6 framework unveiling example demonstration [01]: initial programming experience](/img/3b/0ae9fbadec5dbdfc6981f1f55546a5.jpg)
ASP. Net core 6 framework unveiling example demonstration [01]: initial programming experience

College Students' innovation project management system

Flume configuration 4 - customize mysqlsource

Sqoop installation

Logstash、Fluentd、Fluent Bit、Vector? How to choose the appropriate open source log collector

Design and implementation of community hospital information system

Share the newly released web application development framework based on blazor Technology
![[200 opencv routines] 99 Modified alpha mean filter](/img/df/1b7beb6746f416198fc405249852f8.jpg)
[200 opencv routines] 99 Modified alpha mean filter

Kbp206-asemi rectifier bridge kbp206

Three line by line explanations of the source code of anchor free series network yolox (a total of ten articles, which are guaranteed to be explained line by line. After reading it, you can change the
随机推荐
有個疑問 flink sql cdc 的話可以設置並行度麼, 並行度大於1會有順序問題吧?
LeetCode146. LRU cache
Design and implementation of campus epidemic prevention and control system based on SSM
Use UDP to send a JPEG image, and UPD will convert it into the mat format of OpenCV after receiving it
Port, domain name, protocol.
[200 opencv routines] 99 Modified alpha mean filter
[micro service SCG] 33 usages of filters
Performance of calling delegates vs methods
Share the newly released web application development framework based on blazor Technology
问下,这个ADB mysql支持sqlserver吗?
Jd.com 2: how to prevent oversold in the deduction process of commodity inventory?
qrcode:将文本生成二维码
Why is this an undefined behavior- Why is this an undefined behavior?
Acwing game 58 [End]
Spoon inserts and updates the Oracle database, and some prompts are inserted with errors. Assertion botch: negative time
FBO and RBO disappeared in webgpu
Sqoop installation
When sqlacodegen generates a model, how to solve the problem that the password contains special characters?
返回二叉树中两个节点的最低公共祖先
Bumblebee: build, deliver, and run ebpf programs smoothly like silk