Splitting and converting String to int

后端 未结 5 1582
再見小時候
再見小時候 2021-01-12 06:23

I have a problem with my code. I read a couple of numbers of a text-file. For example: Textfile.txt

1, 21, 333

With my following code I wan

5条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-12 07:08

    For folks who come here from a search, this function takes a separated values string and delimiter string and returns an integer array of extracted values.

    int[] parseStringArr( String str, String delim ) {
        String[] strArr = str.trim().split(delim);
        int[] intArr = new int[strArr.length];
        for (int i = 0; i < strArr.length; i++) {
            String num = strArr[i].trim();
            intArr[i] = Integer.parseInt(num);
        }
        return intArr;
    }
    

提交回复
热议问题