当前位置:网站首页>[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 DefaultGroovyMethodsSupport
Class ;
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 :
false
2、 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 10
3、 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 :
Hello
Two 、 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
边栏推荐
- Problem solving: attributeerror: 'nonetype' object has no attribute 'append‘
- Sqoop安装
- Use of kubesphere configuration set (configmap)
- Multi person online anonymous chat room / private chat room source code / support the creation of multiple chat rooms at the same time
- Why is this an undefined behavior- Why is this an undefined behavior?
- Tiny series rendering tutorial
- Elk log analysis system
- El select, El option drop-down selection box
- 1.五层网络模型
- SQL performance optimization skills
猜你喜欢
The latest blind box mall, which has been repaired very popular these days, has complete open source operation source code
Talk about the SQL server version of DTM sub transaction barrier function
Ubantu disk expansion (VMware)
Privatization lightweight continuous integration deployment scheme -- 01 environment configuration (Part 1)
Pat class a 1162 postfix expression
Vb+access hotel service management system
SQL performance optimization skills
Avoid material "minefields"! Play with super high conversion rate
IPv6 experiment
Asemi rectifier bridge 2w10 parameters, 2w10 specifications, 2w10 characteristics
随机推荐
8. Commodity management - commodity classification
When the low alcohol race track enters the reshuffle period, how can the new brand break the three major problems?
1. Five layer network model
PHP cli getting input from user and then dumping into variable possible?
[105] Baidu brain map - Online mind mapping tool
Ask, does this ADB MySQL support sqlserver?
Breaking the information cocoon - my method of actively obtaining information - 3
[micro service SCG] 33 usages of filters
Multi person online anonymous chat room / private chat room source code / support the creation of multiple chat rooms at the same time
Yuan universe also "real estate"? Multiple second-hand trading websites block metauniverse keywords
What is the most effective way to convert int to string- What is the most efficient way to convert an int to a String?
Pytest (5) - assertion
There is a question about whether the parallelism can be set for Flink SQL CDC. If the parallelism is greater than 1, will there be a sequence problem?
Basic authorization command for Curl
Jd.com 2: how to prevent oversold in the deduction process of commodity inventory?
The perfect car for successful people: BMW X7! Superior performance, excellent comfort and safety
Design and implementation of high availability website architecture
IPv6 experiment
Mongodb common commands
Anchor free series network yolox source code line by line explanation four (a total of ten, ensure line by line explanation, after reading, you can change the network at will, not just as a participan