Delete everything after part of a string

The apache commons StringUtils provide a substringBefore method

StringUtils.substringBefore("Stack Overflow - A place to ask stuff", " - ")


For example, you could do:

String result = input.split("-")[0];

or

String result = input.substring(0, input.indexOf("-"));

(and add relevant error handling)


You can use this

String mysourcestring = "developer is - development";
String substring = mysourcestring.substring(0,mysourcestring.indexOf("-"));

it would be written "developer is -"


Kotlin Solution

Use the built-in Kotlin substringBefore function (Documentation):

var string = "So much text - no - more"
string = string.substringBefore(" - ") // "So much text"

It also has an optional second param, which is the return value if the delimiter is not found. The default value is the original string

string.substringBefore(" - ", "fail")  // "So much text"
string.substringBefore(" -- ", "fail") // "fail"
string.substringBefore(" -- ")         // "So much text - no - more"