简体   繁体   English

根据需要在Java中按长度拆分字符串

[英]Split string by length in java as my requirement

I have a String with length 721 like following : 我有一个长度为721的字符串,如下所示:

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 . 一个字符串的长度应为72。在字符串stmnt ,应有10条语句,这意味着总长度为720 The first statement of string stmnt should be following: 字符串stmnt的第一条语句应如下:

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. 我的问题是我需要在定义部分定义时拆分72个字符串的长度

NOTE: I can do individually using String.subString() but need to solution for all string. 注意:我可以单独使用String.subString()但需要解决所有字符串。 Because I need to parse 721 length of string at a time. 因为我需要一次解析721个字符串的长度。

Update: Need regex code that will make 72 length for all string. 更新:需要regex代码,使所有字符串的长度为72。 Condition should be as I described on definition section 条件应如我在定义部分所述

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. 根据Ingo的评论进行编辑。 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. 建议的Regex只是对该数据集实际需要的粗略估计,因此几乎可以肯定需要进一步完善。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM