Split Strings in String Array (Java)

Search over through google about this topic but not too much result.
Therefore, I would like to share my method.
However, should many persons can resolve this better than me.
import java.util.ArrayList;

public class stringTest {
    public static void main (String[] args)
    {
        //假設有一個String array如下
        String [] s={"1001:台泥","1002:台積電","1003:宏電","1004:蘋果"};

        //宣告一個String array以承接.split將字串分開後回傳的array
        String [] a;

        //宣告一個ArrayList,因ArrayList可使用.add將值加入array
        ArrayList list= new ArrayList();

        //宣告一個迴圈,將String array的值一個一個取出,並分開
            for (int j=0; j<s.length;j++){

            //使用.split將字串分隔,並以另一個String array承接
            a=s[j].split(":");

            //因只需要股票代號,故只取array[0],並一個一個加入ArrayList
            list.add(a[0]);
           }

            //印出結果
            System.out.println(list);
        }
    }

留言

這個網誌中的熱門文章

R中Try and Catch的寫法

如何將DSM(NAS)變成Mail Server

使用VBA記錄股市每分鐘的交易記錄