Murtaza Naiyar
Murtaza Naiyar

Reputation: 21

How to Convert a string without any delimiter to a comma delimited string?

I have a file details.txt in which data stored is in this format

        "571955NandhithaF1975-12-222011-12-06Mumbai"    

Columns are first six digit unique id , name , (M/F) Gender , dob,joining date , and location i have to separate this in six columns using comma delimiter !! Please help me in this problem

Upvotes: 0

Views: 133

Answers (2)

Aditya
Aditya

Reputation: 2415

Pass each line into a regex function which contains the below logic :

    String expression = "571955NandhithaF1975-12-222011-12-06Mumbai";

    Pattern pattern = Pattern
            .compile("([0-9]{6})([a-zA-Z]+)([M|F])([0-9]{4}-[0-9]{2}-[0-9]{2})([0-9]{4}-[0-9]{2}-[0-9]{2})([a-zA-Z0-9]+)");
    Matcher matcher = pattern.matcher(expression);
    if (matcher.find()) {
        //System.out.println(matcher.group());
        System.out.println(matcher.group(1));
        System.out.println(matcher.group(2));
        System.out.println(matcher.group(3));
        System.out.println(matcher.group(4));
        System.out.println(matcher.group(5));
        System.out.println(matcher.group(6));
    }

output:

571955
Nandhitha
F
1975-12-22
2011-12-06
Mumbai

Upvotes: 1

NCM
NCM

Reputation: 1

571955NandhithaF1975-12-222011-12-06Mumbai

To split this type of data, we have to use String Functions in java in the mapper class under map method.
You can use substring(beginindex,endindex) method to get Id of from the string, its like string id[6]=substring(0,5) which returns 6 digit string that is ID.(As ID is Fixed length we take 6)

You can use substring(beginindex) to get remaining string.

Next on wards you have to use REGXP in java.. along with split(regexp) to get the name, gender, dob, doj, loc.
But definitely some workout with java takes place.

go through this link for String functions in java.



Hope this post may help. If any suggestions or modifications to the same are also accepted :)

Upvotes: 0

Related Questions