Java String Split by Regex splitToLongList(String str, String regex)

Here you can find the source of splitToLongList(String str, String regex)

Description

split a string to a List with the given regular expression.

License

Open Source License

Parameter

Parameter Description
str a parameter
regex a parameter

Declaration

public static List<Long> splitToLongList(String str, String regex) 

Method Source Code


//package com.java2s;
/*//www  . ja  va 2s  .  co  m
 * Copyright 2011 Alibaba.com All right reserved. This software is the
 * confidential and proprietary information of Alibaba.com ("Confidential
 * Information"). You shall not disclose such Confidential Information and shall
 * use it only in accordance with the terms of the license agreement you entered
 * into with Alibaba.com.
 */

import java.util.ArrayList;
import java.util.List;

public class Main {
    /**
     * split a string to a List<Long> with the given regular expression.
     * 
     * @param str
     * @param regex
     * @return
     */
    public static List<Long> splitToLongList(String str, String regex) {
        String[] strList = str.split(regex);
        return convertStrArrayToLongList(strList);
    }

    /**
     * @param idList
     */
    public static List<Long> convertStrArrayToLongList(String[] strList) {
        List<Long> longList = new ArrayList<Long>();
        for (String str : strList) {
            long l = 0L;
            try {
                l = Long.parseLong(str);
            } catch (NumberFormatException e) {
                return null;
            }
            longList.add(l);
        }
        return longList;
    }
}

Related

  1. splitNoEmpty(String sStr, String regex)
  2. splitString(String str, List list, String regex)
  3. splitStringToList(String input, String regex)
  4. splitTerms(String text, String pattern)
  5. splitToList(String s, String regex)