Tas Lat Balel
Tas Lat Balel

Reputation: 1

How to extract substring from a griven string in java

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

Answers (2)

hqt
hqt

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

Achintya Jha
Achintya Jha

Reputation: 12843

Try this:

String result = var.substring(0,var.indexOf("["));

Upvotes: 2

Related Questions