Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

To get SubString in Groovy separated By a Character

Tags:

groovy

Consider the below String

String names = "Bharath-Vinayak-Harish-Punith"

I want to get output in the form of string it contains only Bharath. (String up to the first occurrence of "-" operator). Anyone can please tell me, how can be we do this?

like image 424
Bharath A N Avatar asked Jun 29 '12 06:06

Bharath A N


1 Answers

You could use split:

def theName = names.split(/-/)[0]

split returns a String array, then get the first array element.

like image 183
Kelly Avatar answered Oct 07 '22 18:10

Kelly