I have a string like "~~banana~apple~". I want to split that string with "~" is seperator and i want last element in the array after split is "" not "apple".
String fruits = "~~banana~apple~";
String[] arr = fruits.split("~");
String last = ""; //Last string i need
if (arr.length > 0 )
last = arr[arr.length-1];
System.out.println("last: " + last);
//The result
last: apple
//but i want in this case the value of last must be "" because afer "apple" have "~".
Sorry my bad english.
I'm guessing that you are looking for split("~",-1)
. By default split
removes empty strings ( ""
) from the end, but with negative limit it will leave them.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.