Reputation: 1
How to I extract the following substring from a string in Java.
string var=fill x [0,4,4,4] #1000ff
I want to extract only the fill x
(with whitespaces).
Can anyone help me out?
Upvotes: 0
Views: 152
Reputation: 30266
String subString = var.substring(begin, end + 1);
// begin and end is the integer index of var string where you want to get
in your example, it should be:
String subString = var.substring(0, var.indexOf("[")); // if include white space
String subString = var.substring(0, var.indexOf("x") + 1); // if not include white space
Upvotes: 0
Reputation: 12843
Try this:
String result = var.substring(0,var.indexOf("["));
Upvotes: 2