{"id":2226,"date":"2023-05-06T21:48:50","date_gmt":"2023-05-06T13:48:50","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=2226"},"modified":"2023-05-07T10:29:38","modified_gmt":"2023-05-07T02:29:38","slug":"groovy-code-example-string","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/05\/06\/groovy-code-example-string\/","title":{"rendered":"Groovy\u4ee3\u7801\u793a\u4f8b &#8211; \u5b57\u7b26\u4e32"},"content":{"rendered":"<h2>groovy\u5b57\u7b26\u4e32\u57fa\u672c\u7528\u6cd5<\/h2>\n<p>groovy\u5b57\u7b26\u4e32\u7684\u5b9a\u4e49\u4ee5\u53ca\u5e38\u7528\u65b9\u6cd5\u7684\u4f7f\u7528\u793a\u4f8b<\/p>\n<pre><code class=\"language-java\">def name = &#039;John&#039; \/\/ \u5b9a\u4e49\u5b57\u7b26\u4e32\nprintln name \/\/\u6253\u5370\u5b57\u7b26\u4e32\n\/\/\u83b7\u53d6\u5b57\u7b26\u4e32\u7684\u957f\u5ea6\nprintln &#039;The quick brown fox jumps over the lazy dog&#039;.length() \/\/ 43\n\/\/\u83b7\u53d6\u5b57\u7b26\u4e32\u4e2d\u5b50\u5b57\u7b26\u4e32\u7684\u4f4d\u7f6e\nprintln &#039;Non-Blocking IO&#039;.indexOf(&quot;o&quot;) \/\/ 1\n\/\/\u5b57\u7b26\u4e32\u7684\u622a\u53d6\nprintln &#039;AngularJS Service vs Factory vs Provider&#039;.substring(32) \/\/ Provider\n\/\/\u5b57\u7b26\u4e32\u7684\u66ff\u6362\nprintln &#039;C# is the best&#039;.replace(&#039;C#&#039;, &#039;Java&#039;) \/\/ Java is the best\n\/\/\u5b57\u7b26\u4e32\u8f6c\u5927\u5199\nprintln &#039;I am very angry&#039;.toUpperCase() \/\/ I AM VERY ANGRY<\/code><\/pre>\n<h2>\u5b57\u7b26\u4e32\u62fc\u63a5\u4e0eGString<\/h2>\n<pre><code class=\"language-java\">def name = &#039;John&#039;\ndef surname = &#039;Doe&#039;\n\/\/\u5b57\u7b26\u4e32\u62fc\u63a5\nprintln &#039;Hello &#039; + name + &#039; &#039; + surname \/\/ Hello John Doe\n\n\/\/GString\u7684\u4f7f\u7528\ndef name = &#039;John&#039;\ndef surname = &#039;Doe&#039;\n\/\/${varName} \u53d8\u91cf\u5360\u4f4d\u7b26\nprintln &quot;Hello ${name} ${surname}&quot; \/\/ Hello John Doe\nprintln &#039;Hellow ${name} ${surname}&#039; \/\/ Hellow ${name} ${surname}<\/code><\/pre>\n<h2>\u5b57\u7b26\u4e32\u64cd\u4f5c\u7b26<\/h2>\n<p>groovy\u5b57\u7b26\u4e32\u7684\u52a0\u51cf\u4e58\u64cd\u4f5c\u7b26\u4f7f\u7528\u4ee3\u7801\u793a\u4f8b<\/p>\n<pre><code class=\"language-java\">\/\/\u201c-\u201d \u51cf\u64cd\u4f5c\nprintln &#039;I am very long sentence&#039; - &#039;very long &#039; \/\/ I am sentence\n\/\/\u201c+\u201d \u52a0\u64cd\u4f5c\nprintln &#039;I will &#039; + &#039;be very long sentence&#039; \/\/ I will be very long sentence\n\/\/\u201c*\u201d \u4e58\u64cd\u4f5c\nprintln &#039;Ice &#039; * 2 + &#039; baby&#039; \/\/ Ice Ice  baby<\/code><\/pre>\n<p>\u4f7f\u7528\u4e09\u4e2a\u53cc\u5f15\u53f7\u5b9a\u4e49\u591a\u884c\u6587\u672c\uff0c\u5982\u4e0b\u4f8b\u6240\u793a\uff1a<\/p>\n<pre><code class=\"language-java\">def multiLine = &quot;&quot;&quot;\n    Hi everyone, I will\n    write lots of things here\n    because I am not restricted with\n    one line. I am capable of\n    multi lines\n&quot;&quot;&quot;\nprintln multiLine<\/code><\/pre>\n<p>\u4e5f\u53ef\u4ee5\u652f\u6301\u4e09\u4e2a\u5355\u5f15\u53f7\u7684\u591a\u884c\u6587\u672c\u5b9a\u4e49\uff0c\u5982\u4e0b\u4f8b\u6240\u793a\uff1a<\/p>\n<pre><code class=\"language-java\">def multiLine1 = &#039;&#039;&#039;\n    Hi everyone, I will\n    write lots of things here\n    because I am not restricted with\n    one line. I am capable of\n    multi lines\n&#039;&#039;&#039;\nprintln multiLine1<\/code><\/pre>\n<p>\u5176\u4e2d\uff0c\u901a\u8fc7<code>&#039;&#039;&#039;<\/code>\u5b9a\u4e49\u7684\u6587\u672c\u4e0d\u652f\u6301\u53d8\u91cf\u5360\u4f4d\u7b26<code>${varName}<\/code><\/p>\n<h2>\u5b57\u7b26\u4e32\u5206\u5272<\/h2>\n<pre><code class=\"language-java\">\/\/groovy\u7684\u5b57\u7b26\u4e32\u5206\u5272\u6bd4\u8f83\u667a\u80fd\uff0c\u4f1a\u81ea\u52a8\u5224\u65ad\u7a7a\u683c\u6216tab\u5b57\u7b26\u5206\u5272\ndef text = &#039;Hello World&#039;\n\/\/\u652f\u6301\u7a7a\u683c\u5206\u5272\nprintln text.tokenize() \/\/ [Hello, World]\n\ndef textWithComma = &#039;Hello,World&#039;\n\/\/\u4f7f\u7528\u9017\u53f7\u5206\u5272\nprintln textWithComma.tokenize(&#039;,&#039;) \/\/ [Hello, World]\n\n\/\/\u652f\u6301tab\u5206\u5272\ndef textWithTab = &#039;Hello    World&#039;\nprintln textWithTab.tokenize()<\/code><\/pre>\n<h2>\u5b57\u7b26\u4e32\u622a\u53d6substring<\/h2>\n<p>\u5b57\u7b26\u4e32\u622a\u53d6\u9700\u8981\u7ecf\u5e38\u4f7f\u7528\u5230\uff0c\u5728groovy\u4e2d\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u622a\u53d6\u5b57\u7b26\u4e32\u3002<\/p>\n<p>1\u3001\u4f7f\u7528substring\u65b9\u6cd5\uff0c\u540cjava\u4e2d\u7684\u5b57\u7b26\u4e32\u622a\u53d6<\/p>\n<pre><code class=\"language-java\">def log = &quot;Exception on saving user with username:johntheripper&quot;\n\ndef username = log.substring(log.lastIndexOf(&quot;:&quot;) + 1, log.length())\nprintln username \/\/ johntheripper\n\ndef usernameWithoutEndIndex = log.substring(log.lastIndexOf(&quot;:&quot;) + 1)\nprintln usernameWithoutEndIndex \/\/ johntheripper<\/code><\/pre>\n<p>2\u3001\u4f7f\u7528subSequence\u65b9\u6cd5\uff0c\u4e5f\u662fJava\u4e2d\u7684\u539f\u751f\u65b9\u6cd5<\/p>\n<pre><code class=\"language-java\">def log = &quot;Exception on saving user with username:johntheripper&quot;\ndef username = log.subSequence(log.lastIndexOf(&quot;:&quot;) + 1, log.length())\nprintln username \/\/ johntheripper<\/code><\/pre>\n<p>3\u3001groovy\u98ce\u683c\u7684\u5b57\u7b26\u4e32\u622a\u53d6<\/p>\n<p>\u63a5\u4e0b\u6765\u6211\u4eec\u53ef\u4ee5\u4e86\u89e3\u5230groovy\u98ce\u683c\u7684\u5b57\u7b26\u4e32\u622a\u53d6\u3002<\/p>\n<pre><code class=\"language-java\">def text1 = &quot;My last character will be removed soon&quot;\nprintln text1[0..-2] \/\/ My last character will be removed soo\n\ndef text2 = &quot;My first word will be removed soon&quot;;\nprintln text2[3..-1] \/\/ first word will be removed soon\n\ndef text3 = &quot;noos em daer lliw uoy ,tneitap eB&quot;\nprintln text3[-1..0] \/\/ Be patient, you will read me soon<\/code><\/pre>\n<p>\u53ef\u4ee5\u770b\u5230\uff0cgroovy\u98ce\u683c\u7684\u5b57\u7b26\u4e32\u622a\u53d6\u65e2\u7b80\u5355\u53c8\u5bb9\u6613\u7406\u89e3\uff0c\u901a\u8fc7<code>[]<\/code>\u53f7\u65b9\u5f0f\u5b9a\u4e49\u622a\u53d6\u65b9\u5f0f\u3002\u6bd4\u5982<code>0..5<\/code>\u4ee3\u8868\u7740\u7d22\u5f15\u53f7\u4ece0\u52305\u7684\u5b57\u7b26\u7ec4\u6210\u7684\u5b57\u7b26\u4e32\u3002\u7d22\u5f15\u53f7\u53ef\u4ee5\u662f\u8d1f\u6570\uff0c\u5982\u679c\u7ed3\u675f\u7d22\u5f15\u53f7\u4e3a\u8d1f\u6570\u5219\u8868\u660e\u4ece\u5c3e\u90e8\u5f00\u59cb\u622a\u53d6\u3002\u5982\u679c\u8d77\u59cb\u7d22\u5f15\u53f7\u4e3a\u8d1f\u6570\uff0c\u5219\u8868\u660e\u662f\u53cd\u5411\u622a\u53d6\uff0c\u622a\u53d6\u7684\u5b57\u7b26\u4e32\u4e5f\u662f\u53cd\u5e8f\u7684\u3002<\/p>\n<p>4\u3001\u4f7f\u7528getAt\u65b9\u6cd5\u622a\u53d6\u5b57\u7b26\u4e32<\/p>\n<pre><code class=\"language-java\">def text1 = &quot;crazy fox jumps over lazy dog&quot;\nprintln text1.getAt(0..(text1.length() - 5)) \/\/ crazy fox jumps over lazy\n\ndef text2 = &quot;keep calm and carry on&quot;\nprintln text2.getAt(-1..5) \/\/ no yrrac dna mlac<\/code><\/pre>\n<p>getAt\u540c<code>[]<\/code>\u622a\u53d6\u662f\u4e00\u81f4\u7684\uff0c\u4e5f\u540c\u6837\u652f\u6301\u8d1f\u6570\u3002<\/p>\n<p>5\u3001\u5b57\u7b26\u4e32\u51cf\u6cd5\u64cd\u4f5c<\/p>\n<pre><code class=\"language-java\">def text1 = &quot;Sorry, I need to separate from you&quot;\nprintln text1 - &quot; you&quot; \/\/ Sorry, I need to separate from\n\ndef text2 = &quot;Minus string usage&quot;\nprintln text2.minus(&quot; usage&quot;) \/\/ Minus string<\/code><\/pre>\n<p>\u53ef\u4ee5\u4f7f\u7528\u51cf\u6cd5\u64cd\u4f5c\u4ece\u5b57\u7b26\u4e32\u4e2d\u53bb\u6389\u6307\u5b9a\u5b57\u7b26\u4e32\u3002<\/p>\n<p>\u5728groovy\u4e2d\u5b57\u7b26\u4e32\u7684\u622a\u53d6\u662f\u975e\u5e38\u65b9\u4fbf\u7684\uff0c\u4f60\u65e2\u53ef\u4ee5\u50cf\u4f7f\u7528java\u90a3\u6837\u53bb\u4f7f\u7528\u4e5f\u53ef\u4ee5\u4f7f\u7528groovy\u98ce\u683c\u7684\u8bed\u6cd5\u622a\u53d6\u5b57\u7b26\u4e32\u3002<\/p>\n<h2>\u683c\u5f0f\u5316\u8f93\u51fa\u5b57\u7b26\u4e32<\/h2>\n<p>groovy\u4e2d\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u7684\u65b9\u5f0f\u4e0ejava\u7c7b\u4f3c\uff0c\u53c2\u8003\u4ee5\u4e0b\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"language-java\">def x = 66\n\ndef res = sprintf(&quot;value: %s&quot;, x)   \/\/ \u4f5c\u4e3a\u5b57\u7b26\u4e32\nprintln(res)\nprintln(sprintf(&quot;value: %d&quot;, x))    \/\/ \u4f5c\u4e3a\u6570\u5b57\nprintln(sprintf(&quot;value: %c&quot;, x))    \/\/ \u4f5c\u4e3a\u5b57\u7b26\n\n\/\/ \u4f7f\u75280\u8865\u9f50\nprintf(&#039;d\\n&#039;, x)\nprintln( sprintf(&#039;d&#039;, x) )\n\n\/\/ \u6307\u793a\u53c2\u6570\u4f4d\u7f6e\nnames = [&#039;First&#039;, &#039;Second&#039;, &#039;Third&#039;, &#039;Fourt&#039;]\nprintln( sprintf(&#039;%2$s %3$s %3$s %1$s&#039;, names) )<\/code><\/pre>\n<p>\u7c7b\u4f3c\u7684printf\u7684\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-java\">def v = 65\nprintf(&quot;&lt;%s&gt;\\n&quot;, v);    \/\/ &lt;65&gt;\nprintf(&quot;&lt;s&gt;\\n&quot;, v);    \/\/ &lt;      65&gt;\nprintf(&quot;&lt;%-10s&gt;\\n&quot;, v);    \/\/ &lt;65      &gt;\nprintf(&quot;&lt;%c&gt;\\n&quot;, v);    \/\/ &lt;A&gt;\nprintf(&quot;&lt;%d&gt;\\n&quot;, v);    \/\/ &lt;65&gt;\nprintf(&quot;&lt;d&gt;\\n&quot;, v);    \/\/ &lt;00065&gt;<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>groovy\u5b57\u7b26\u4e32\u57fa\u672c\u7528\u6cd5 groovy\u5b57\u7b26\u4e32\u7684\u5b9a\u4e49\u4ee5\u53ca\u5e38\u7528\u65b9\u6cd5\u7684\u4f7f\u7528\u793a\u4f8b def name = &#039; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[568],"tags":[567],"class_list":["post-2226","post","type-post","status-publish","format-standard","hentry","category-groovy","tag-groovy"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/2226","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=2226"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/2226\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=2226"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=2226"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=2226"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}