Yubaraj
Yubaraj

Reputation: 3988

Split string by length in java as my requirement

I have a String with length 721 like following :

String stmnt="27/08/2013 Cr 00000000000288.70 AMOUNT DEPOSITED FOR TELEPHONE EXPENSES  22/08/2013 Dr 00000000085000.00 PMT TO abcde BOHARA                     20/08/2013 Cr 00000000015844.38 BEING REFUND AMOUNT DEPOSITED OF RMDC IP20/08/2013 Cr 00000000047419.09 BEING REFUND AMOUNT DEPOSITED OF SKBBL I15/08/2013 Dr 00000000002900.00 PMT TO lkjhgd 09876543                  12/08/2013 Dr 00000000001723.00 PMT TO SELF                             12/08/2013 Cr 00000000025563.00 liuytrew /SALARY / 070 / 748392 lkoiuytr08/08/2013 Dr 00000000002000.00 PMT TO SELF                             07/08/2013 Dr 00000000000500.00 PMT TO 123456 nmnbvsgd                  29/07/2013 Cr 00000000002000.00 DE PO BY SELF                           ";

One string should be length of 72. In string stmnt there should be 10 statements it means total length 720. The first statement of string stmnt should be following:

string should be ="27/08/2013 Cr 00000000000288.70 AMOUNT DEPOSITED FOR TELEPHONE EXPENSES ";

But the problem is there is padding one more space like following:

irritated string="27/08/2013 Cr 00000000000288.70 AMOUNT DEPOSITED FOR TELEPHONE EXPENSES  ";

on first string so it is the problem that is irritating to me.

The definition of one string as follows:

 1. first 10 digits are : date with length 10 and startIndex 0
 2. next 4 digits are transaction type (CR/DR) `including spaces
    also`  and startIndex 10
 3. next 18 digits are Account Balance including one space on right
    side and startIndex 14
 4. and rest 40 digits are particular  startIndex 32

My Question is I need to split length of 72 string as defining on definition section.

NOTE: I can do individually using String.subString() but need to solution for all string. Because I need to parse 721 length of string at a time.

Update: Need regex code that will make 72 length for all string. Condition should be as I described on definition section

Upvotes: 0

Views: 222

Answers (1)

Charles Forsythe
Charles Forsythe

Reputation: 1861

I would use a regular expression like the code below. I haven't tested this, so you'll need to tweak it. The benefit of using the regex is that is will not "mind" if there are extra spaces between the fixed-length records.

p = Pattern.compile("(\\d{2}/\\d{2}/\\d{4} (Cr|Dr) \\d{14}\\.\\d{2} .{50})");
m = p.matcher(bigRecord);

while(m.find()) {
    String record = m.group();
}

Edited per Ingo's comment. The suggested Regex is just a rough estimate of what would actually be needed for this data set, so further refinements are almost certainly required.

Upvotes: 4

Related Questions