0

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.

5
  • If apple isn't what you want, what do you want? Commented Dec 7, 2013 at 16:09
  • The value is in fruits variable and you are splitting ten variable. Is that a typo or partial code. Commented Dec 7, 2013 at 16:10
  • There is nothing after your last ~ so there's nothing to point to as the last element. Commented Dec 7, 2013 at 16:11
  • @Jeroen Vannevel i want it must be an empty string because after apple have "~" Commented Dec 7, 2013 at 16:12
  • @Subir Kumar Sao Sorry i copy and forget to edit to English varriable (ten = Name in English) Commented Dec 7, 2013 at 16:15

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.