Groovy Tip 10 Groovy语言对String的简化操作

本文介绍Groovy语言中简化字符串操作的方法,包括索引访问、子串获取、字符删除等功能,对比Java,Groovy提供了更直观简洁的语法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

           Groovy Tip 10 Groovy语言对String的简化操作
 
在Groovy语言中,对String对象的操作就像操作List对象一样的方便。比如我们在Java语言中,对String对象的操作用得比较多的是substring方法。如下:
       String str = "abcdefg";
      
    System.out.println(str.substring(1,3));
 
结果为:
bc
 
这里有两点不便:一是substring方法和参数的繁琐;二是它的最后一个参数,比如substring(1,3)得到的结果不是str对象的第一位到第三位,而是第二位。这和我们的直觉不符,常常导致我们出错。
而在Groovy语言里,上面的代码被改造为下面的样子:
     def str = 'abcdefg'
     
 println str[1..2]
 
结果为:
bc
 
看到了吧,像数组一样操作String对象,str[1..2]得到的就是第一位到第二位的值,符合我们的习惯。
当然,你还可以取String对象中的任意字符:
     def str = 'abcdefg'
     
 println str[1,3,5]
 
结果是:
bdf
 
很棒吧!
既然操作String对象就像操作List对象一样,那么List类的一些方法如eachString对象就可以直接拿来使用了。
     def str = 'abcdefg'
     
     str.each{
        print it
        print','
 }
 
结果为:
a,b,c,d,e,f,g,
 
     def str = 'abcdefg'
     
 println str.contains('d')
 
结果为:
true
 
等等。其他方法,如“find”、“findAll”、“every”、“any”等方法都可以在String对象中使用。
如果你不喜欢str对象后面的“fg”两个字符,想把他们从str对象中删掉,在Java语言中,你必须这样做:
       String str = "abcdefg";
      
       str = str.substring(0,5);
      
    System.out.println(str);
 
而在Groovy语言中,你可以这样做:
     def str = 'abcdefg'
     
     str = str-'fg'
     
 println str
 
当然,你也可以把str对象中的“ab”减去:
     def str = 'abcdefg'
     
     str = str-'ab'
     
 println str
 
结果为:
cdefg
 
如果在str对象中减去“st”呢:
     def str = 'abcdefg'
     
     str = str-'st'
     
 println str
 
结果为:
abcdefg
 
如果在str对象中减去“be”呢:
     def str = 'abcdefg'
     
     str = str-'be'
     
 println str
 
结果为:
abcdefg
D:/software/Gradle/wrapper/dists/gradle-8.4-all/caches/transforms-3/1c7ee2effc4702b7612f760440e420dd/transformed/jetified-databinding-ktx-8.3.2-api.jar!/META-INF/databindingKtx_release.kotlin_module: Module was compiled with an incompatible version of Kotlin. The binary version of its metadata is 1.9.0, expected version is 1.7.1. D:\wordCode\HiAppProject\HiApp\build\intermediates\packaged_manifests\envBetaDebug\AndroidManifest.xml:347: warn: unexpected element <intent-downloadFilter> found in <manifest><application><activity>. D:\wordCode\HiAppProject\HiApp\build\intermediates\packaged_manifests\envBetaDebug\AndroidManifest.xml:679: warn: unexpected element <state> found in <manifest><application><receiver><intent-filter>. D:\wordCode\HiAppProject\HiApp\build\intermediates\packaged_manifests\envBetaDebug\AndroidManifest.xml:743: warn: unexpected element <state> found in <manifest><application><receiver><intent-filter>. D:\wordCode\HiAppProject\HiApp\build\intermediates\packaged_manifests\envBetaDebug\AndroidManifest.xml:1344: warn: unexpected element <state> found in <manifest><application><receiver><intent-filter>. D:\wordCode\HiAppProject\HiApp\build\intermediates\packaged_manifests\envBetaDebug\AndroidManifest.xml:1359: warn: unexpected element <state> found in <manifest><application><receiver><intent-filter>. warn: removing resource com.huawei.appmarket:plurals/agguard_find_n_risk_app without required default value. warn: removing resource com.huawei.appmarket:plurals/agguard_recent_scan without required default value. warn: removing resource com.huawei.appmarket:plurals/agguard_recent_scan_title_all_apps without required default value. warn: removing resource com.huawei.appmarket:plurals/agguard_recent_scan_title_has_virus without required default value. warn: removing resource com.huawei.appmarket:plurals/agguard_safety_report_chart_processed without required default value. warn: removing resource com.huawei.appmarket:plurals/agguard_scan_item_unused_apps_tips_more without required default value. warn: removing resource com.huawei.appmarket:plurals/agguard_sync_fa_unsafe without required default value. warn: removing resource com.huawei.appmarket:plurals/agguard_tab_scan_unsafe_bottom_tip without required default value. warn: removing resource com.huawei.appmarket:plurals/agguard_unknown_app_card_content_more without required default value. warn: removing resource com.huawei.appmarket:plurals/appcomment_accessibility_disliked without required default value. warn: removing resource com.huawei.appmarket:plurals/appcomment_accessibility_like_zero without required default value. warn: removing resource com.huawei.appmarket:plurals/appcomment_accessibility_liked without required default value. warn: removing resource com.huawei.appmarket:plurals/appzone_comment_count without required default value. warn: removing resource com.huawei.appmarket:plurals/cancel_ignore_desc without required default value. warn: removing resource com.huawei.appmarket:plurals/detail_comment_commented_people without required default value. warn: removing resource com.huawei.appmarket:plurals/downloadedreport_app_limited_ex without required default value. warn: removing resource com.huawei.appmarket:plurals/downloadedreport_awardtype_point_ex without required default value. warn: removing resource com.huawei.appmarket:plurals/forum_cards_sort_title without required default value. warn: removing resource com.huawei.appmarket:plurals/image_selected_count_title without required default value. warn: removing resource com.huawei.appmarket:plurals/media_selected_count without required default value. warn: removing resource com.huawei.appmarket:plurals/pics_num_tips without required default value. warn: removing resource com.huawei.appmarket:plurals/server_upgrades_prompt_minute_format without required default value. warn: removing resource com.huawei.appmarket:plurals/update_notify_title_ex_new without required default value. warn: removing resource com.huawei.appmarket:plurals/video_num_tips without required default value. warn: removing resource com.huawei.appmarket:plurals/video_selected_count_title without required default value. warn: removing resource com.huawei.appmarket:plurals/weibo_share_word without required default value. warn: removing resource com.huawei.appmarket:string/about_fans_qq_group without required default value. warn: removing resource com.huawei.appmarket:string/about_weixin_account without required default value. warn: removing resource com.huawei.appmarket:string/about_xinweibo_account without required default value. warn: removing resource com.huawei.appmarket:string/agguard_add_security_control_explanation without required default value. warn: removing resource com.huawei.appmarket:string/agguard_app_intercept_description without required default value. 讲解报错
最新发布
07-01
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值