在groovy中获取标记化字符串中的最后一个单词 [英] getting last word in a tokenized string in groovy
本文介绍了在groovy中获取标记化字符串中的最后一个单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
一个基本的字符串处理问题..我有一个像val1.val2.val3 ..... valN这样的标记化字符串如何从上面的字符串中获取最后一个单词valN.
A basic String handling question..I have a tokenized String like val1.val2.val3.....valN How do I get the last word valN from the string above.
推荐答案
If you pass a negative index n to the subscript operator in a List, you get n-th last element. Therefore, the -1 element is the last one:
def words = 'val1.val2.val3'
def last = words.tokenize('.')[-1]
assert last == 'val3'
Update: You also have the, arguably more readable, last
method:
def last = words.tokenize('.').last()
这篇关于在groovy中获取标记化字符串中的最后一个单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文