-6

Possible Duplicate:
How do I split a string with any whitespace chars as delimiters?

I want to break up a string in java. I have a string "message.txt.cpabe".I want to remove the last portion and only want "message.txt". How do I do it?

Community
  • 1
  • 1
Poonam Hoshi
  • 71
  • 1
  • 3
  • 8

4 Answers4

6
String s = "message.txt.cpabe";
int indexOfLast = s.lastIndexOf(".");
String newString = s;
if(indexOfLast >= 0) newString = s.substring(0, indexOfLast);
System.out.println(newString); // prints "message.txt"
Eng.Fouad
  • 115,165
  • 71
  • 313
  • 417
2

I would use LastIndexOf(".") to get the index of that last '.', and then use substring() to cut out the part that you want.

Oleksi
  • 12,947
  • 4
  • 56
  • 80
1
String str="message.txt.cpabe";
str=str.substring(0,str.lastIndexOf("."));

Try this.

ganesshkumar
  • 1,317
  • 1
  • 16
  • 35
-2

Probably not what you wanted but certainly what you asked for.. :-)

String original = "message.txt.cpabe";
original = "message.txt";
jabal
  • 11,987
  • 12
  • 51
  • 99