1

I have a String

Millett Angle-Loc Weaver Extension 1 inch Rings, Black, Medium, Front/Rear Exte

I would like to extract "Millett Angle-Loc Weaver Extension 1 inch Rings, Black, Medium, Front/Rear" from this String, ie, the substring before the last " "(space/blank) (or first from right).

Could someone please provide some help?

ifloop
  • 8,079
  • 2
  • 26
  • 35
yogesh9239
  • 2,910
  • 3
  • 15
  • 11

2 Answers2

2
final String str = "Millett Angle-Loc Weaver Extension 1 inch Rings, Black, Medium, Front/Rear Exte";

System.out.printf("%s\n", str.substring(0, str.lastIndexOf(' ')));

This prints:

Millett Angle-Loc Weaver Extension 1 inch Rings, Black, Medium, Front/Rear

ifloop
  • 8,079
  • 2
  • 26
  • 35
  • @yogesh9239 You are welcome! You can mark an answer as accepted if it helped and satisfied you. – ifloop Apr 09 '14 at 08:11
1

Assuming your input String is str, try this. It should do it.

int i = str.lastIndexOf(' ');
String str1 = i >= 0 ? str.substring(0, i) : "";
String str2 = str.substring(i + 1);
System.out.println(str1);   
System.out.println(str2);   

See also: String.lastIndexOf

peter.petrov
  • 38,363
  • 16
  • 94
  • 159