Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to split string, remove last element and join back in Java?

Tags:

java

I have a array variable filesFound that holds a filename. How do I go about removing the last number part including its extension.

...
File[] filesFound = SomeUtils.findFile("xyz","c:\\") 

//fileFound[0] is now "abc_xyz_pqr_27062016.csv"
//What I need is "abc_xyz_pqr" only

String[] t = filesFound[0].toString().split("_")
Arrays.copyOf(t, t.length - 1) //this is not working
...
like image 808
Bala Avatar asked Jul 27 '16 14:07

Bala


1 Answers

Arrays.copyOf returns a new array, so you have to assign it to t or a new variable:

t = Arrays.copyOf(t, t.length - 1)
like image 51
Jens Avatar answered Sep 18 '22 11:09

Jens