how to remove last word in a string
word should be dynamic
for example:-String [] a={"100-muni-abc"};
i want output like this 100-muni
remove last one-abe
Asked
Active
Viewed 1.4k times
-5

gefei
- 18,922
- 9
- 50
- 67

muneshwar reddy
- 1
- 1
- 1
- 2
-
10have you tried anything? – Reimeus Sep 02 '15 at 19:10
4 Answers
2
Try:
String a = "100-muni-abc";
String res = a.substring(0, a.lastIndexOf("-"));

Jean Logeart
- 52,687
- 11
- 83
- 118
1
Starting from
String str = "100-muni-abc";
Removing the last char
str = str.substring(0,str.length() - 1)
Removing the last 3 chars
str = str.substring(0,str.length() - 3)
A bit late huh?

NiB
- 801
- 1
- 9
- 26
0
Try this
String str = "100-muni-abc";
String [] parts = str.split("-");
System.out.println(parts[0]+"-"+parts[1]);

optimus_Prime
- 21
- 5
-1
Try this
- String text = What is the name of your first love?;
- String lastWord = text.substring(text.lastIndexOf(" ")+1);
- System.out.println(lastWord);
Output Answer : love?

Ritesh Kumar
- 11
- 1