How to read a line from a CSV file and split it?

I have a .csv file that I should read a string and break it into a substring, for example s1:s2:s3.
Then I have to smash it into s1 s2 s3.
Separation based on ":".

0
source share
1 answer

Well, try the following beanshell code (= java) to parse the extracted "subject string" variable into separate "subject" variables (from BeanShell Sampler for example):

String line = vars.get("vSubjects");

if(line != null) {
    StringTokenizer st = new StringTokenizer(line, ":");

    int i = 0;
    while (st.hasMoreTokens()) {
        String subj = st.nextToken();

        i++;
        String varname = vars.get("vName") + "_subj_" + i;
        vars.put(varname,subj);
    }
}

So, you will get variables for each line analyzed, such as the following (you can use Debug Sampler for monitoring):

John_subj_1=Maths
John_subj_2=Science
John_subj_3=History
. . .
vAge=23
vGender=Male
vName=John
vSubjects=Maths:Science:History
+1
source

All Articles