Java: Get last element after split

后端 未结 12 1819
慢半拍i
慢半拍i 2020-12-02 11:31

I am using the String split method and I want to have the last element. The size of the Array can change.

Example:

String one = \"Dü         


        
12条回答
  •  心在旅途
    2020-12-02 12:28

    using a simple, yet generic, helper method like this:

    public static  T last(T[] array) {
        return array[array.length - 1];
    }
    

    you can rewrite:

    lastone = one.split("-")[..];
    

    as:

    lastone = last(one.split("-"));
    

提交回复
热议问题