How to get substring from a string with split, slice or similar methods?

Hey folks,

How can I get substring from a string?

let’s say i got this string returned in a variable:
"I am John Wyck. I live on 123 ave. I have 1 dog named Skip.

This is my phone number 123-456-7890 and my website is

Please call me if you need help"

Now let’s say I want to just get “my webite is” from the string WITHOUT using INDEX…but only string. like get the string between “and” and “Please”

How can i do that?
I’ve tried several way…but seems like only begIndex and endIndex works… I can’t use index in this situation.
I currently use code like below with begIndex and endIndex… but this is different since i can’t use index.
String newString = getContent.split('and')[1].substring(**begIndex, endIndex**).trim()

1 Like

go for

import org.apache.commons.lang3.StringUtils
String newString = StringUtils.substringBetween(variable,"and","please")

This should help


This worked as I expected. I was trying to do exacty this… just hovering around simiar codeblock but couldn’t get it to work IoI…


1 Like