问题描述:

I have a string of comma separated values and that initial string could contain any number of comma separated values. I need to create a program in such a way so that if there is 100 comma separated values in a string then I could get 5 different strings each containing 20 comma separated values. Similarly if there is 200 comma separated values in initial string then I could get 10 strings each having 20 comma separated values.

Here's what I have done to get csv count :

public int getCSVCount(String CSV) {

String[] CSVArray = CSV.split(",");

int number;

if(CSV == null || CSV.isEmpty()){

number = 0;

}else{

number = CSVArray.length;

}

return number;

}

Please help me out with this.

网友答案:
public String[] getString(String CSV) {

    String[] output ;
    String[] CSVArray = CSV.split(",");
    int number;
    if(CSV == null || CSV.isEmpty()){
        number = 0;
    }else{
        number = CSVArray.length;
    }

    Int count = 1;
    Int j = 0;
    for (int i : CSVArray)
    {
        if (count >20)
        {
            j = j+ 1;
            count = 0;
        }
        output[j] = output[j] + ","+ CSVArray[i];
    }

    return output;
}

Here is a simple example to do things. I did not execute it.

My assumption is that you want to have a string with 20 comma separated values.

If the count is 100 then you will have array of 5 string with size of 20 comma separated values

Above will help you

网友答案:

Here you go all tested and working

import java.util.ArrayList;

public class test {

    ArrayList<String> stringOf100 = new ArrayList<String>();
    ArrayList<String> stringOf200 = new ArrayList<String>();

    public test() {


        for (int i = 1; i <= 200; i++) {
        stringOf200.add(i + ",");
    }

    for (int i = 1; i <= 100; i++) {
        stringOf100.add(i + ",");
    }

    stringOf100.size(); // = 100

    for (String string : stringOf100) {
        System.out.println(string);
    }
    /* prints this.
    1,  2,  3,  4,  5,  6,  7,  8,  9,  10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 
    26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 
    52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 
    78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100
    */

    filter(stringOf100);
//      filter(stringOf200);



}

    private void filter(ArrayList<String> strings) {
        int outputCommaValues = 20; //as i understood this is a constant of 20.  "different strings each containing 20 comma separated values."
        int outputStrings = strings.size() / outputCommaValues; //5 or 10 or 5/100 = 0.05 -> 0.05*strings.size if strings.size = 100 -> 0.05*100 = 5 if strings.size = 200 -> 0.05*200 = 10. not sure what you want this to be

        ArrayList<String> output = new ArrayList<String>();

        StringBuilder sb = new StringBuilder();

        int c = 0;
        for (int i = 1; i <= outputStrings; i++) { 
            for (int j = 1; j <= outputCommaValues; j++) {
                if (strings.get(c) != null) {
                    sb.append(strings.get(c));
                    c++;
                } 
            }
            output.add(sb.toString());
            sb.delete(0, sb.length());
        }

        System.out.println(" c: " + c);
        System.out.println(" size: " + output.size());

        for (String string : output) {
            System.err.println(string);
        }

        /* stringOf100 outputs 5 strings:
         1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,            
         21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,   
         41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,   
         61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,   
         81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100,  
         */


        /* stringOf200 outputs 10 strings:
         1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,
         21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,
         41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,
         61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,
         81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100,
         101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,
         121,122,123,124,125,126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,
         141,142,143,144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,
         161,162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179,180,
         181,182,183,184,185,186,187,188,189,190,191,192,193,194,195,196,197,198,199,200,
     */

    }

}
网友答案:

It shouldn't be anything difficult for you to

  1. Split the string by comma
  2. for every 20 string, join them using a comma, and put it in a result array/list

If you are open to third party lib, by making use of, for example, Apache Commons Collections + Commons Lang will make the task very easy

psuedo code:

List<List<String>> csvValueGroups = ListUtils.partition(Arrays.asList(csv.split(',')), 20);
List<String> result = new ArrayList<String>(csvValueGroups.size());
for (List<String> group : csvValueGroups) {
    result.add(StringUtils.join(group, ","));
}
网友答案:

Not checked code

final String SEPERATOR  = " ";
List<String> strings = new      ArrayList<>();
StringBulder builder;
for(int i =0; i<number ;i++){
if((i % 20) ==0){
if(i!=0){
strings.add(builder.toString());
}
builder = new StringBuilder();
}
builder.append(CSVArray[i]);
/* If (i+1) %20 is zero it means it is the last string in this 20 we dont want aeperatot after it */if((i+1)%20) !=0){
builder.append(SEPERATOR);
}
}
相关阅读:
Top