Slice a string in groovy

前端 未结 3 586
长情又很酷
长情又很酷 2021-01-07 16:46

I have a 18 character string I want characters 2-8 from. In python I can do this:

sliceMe = \"nnYYYYYYnnnnnnnnnn\"
print sliceMe[2:8]

prin

相关标签:
3条回答
  • 2021-01-07 16:59

    For future reference, you can compare the "Programming Language Examples Alike Cookbook" strings methods if you are unclear on how something is written in Python versus Groovy (or other syntaxes).

    Here are the slicing python strings http://pleac.sourceforge.net/pleac_python/strings.html

    And here are the slicing groovy strings: http://pleac.sourceforge.net/pleac_groovy/strings.html

    Check the table of contents if you need to see other comparisons, its a good reference.

    0 讨论(0)
  • 2021-01-07 17:07
    groovy:000> sliceMe = "nnYYYYYYnnnnnnnnnn"
    ===> nnYYYYYYnnnnnnnnnn
    groovy:000> sliceMe[2..7]
    ===> YYYYYY
    

    Note the difference in the length being 1 less.

    0 讨论(0)
  • 2021-01-07 17:12

    You inherit all the Java methods off String so sliceMe.substring(2,7) should do the trick.

    0 讨论(0)
提交回复
热议问题