Reputation: 3988
I have a String like:
AB524D000000000000231200000001D0000000000000000524
The length of string is 50. Above string is one. this type of string may have lenght 250 ie. five string example:
AB524D000000000000231200000001D0000000000000000524AB524D000000000000231200000001D0000000000000000524AB524D000000000000231200000001D0000000000000000524AB524D000000000000231200000001D0000000000000000524AB524D000000000000231200000001D0000000000000000524.
Now my requirement is I need to change D
to C
.
I used following code to replace for one string:
String code = key.substring(0, 2);
String currency = key.substring(2, 5);
String type = key.substring(5, 6);
String amount = key.substring(6, 22);
String rate = key.substring(22, 30);
String type2 = key.substring(30, 31);
String rAmount = key.substring(31, 47);
String currency2 = key.substring(47, 50);
String finalReq = code + currency + "C" + amount + rate + "C" + rAmount + currency2;
I got following output:
AB524C000000000000231200000001C0000000000000000524
this is good for one string I mean 50 length string. But string length may 0-250 (string one to 5) but pattern is same like : AB524D000000000000231200000001D0000000000000000524.
Which is the best logic to fulfill my requirement ?.
Note: I can not do string.replaceAll('D','C')
because my zeroth and first index has character I mean it may have also D.
Upvotes: 0
Views: 1120
Reputation: 124215
I would say that
replaceAll("\\G(.{5})D(.{24})D(.{19})", "$1C$2C$3")
should do the trick but I don't know if your string will only contain data in format you described or if you want to replace only D
or any character that can be in places that D
is.
replaceAll
uses regex as first parameter, and String that can use results of that regex as second parameter. In regex
.
dot represents any character except new line.{x}
represents series of any characters that is length x
like .{3}
can match AbZ
or 1X9
,(...)
will create group, and each group has its unique number. This number can be used later for example in replacement
String via $x
where x
is number of group(.{5})D(.{24})D(.{19})
will match any 5 characters (and store them in group 1), then D
then 24 characters (and create store them in group 2) then D
and lastly any 19 characters (and store them in group 3)"$1C$2C$3"
I will use strings that ware matched in first group, then instead of D
will put C
then will include match from group 2, then again instead of D
will put C
and after that include last part of match (last 19 characters after second D
stored in group 3)\\G
represents start of the string or previously match (so there can't be any characters between previous match and current match).Upvotes: 1
Reputation: 1245
if you are sure that every string is 50 char so :
index = finalReq.length() % 50;
for(int i = 0; i<index; i++){
String code = key.substring(0 + 50 * i, 2 + 50 * i);
String currency = key.substring(2 + 50 * i , 5 + 50 * i);
...
replace ...
}
Upvotes: 0
Reputation: 7228
Just use java's String replace method.
String old = "AB524D000000000000231200000001D0000000000000000524AB524D000000000000231200000001D0000000000000000524AB524D000000000000231200000001D0000000000000000524AB524D000000000000231200000001D0000000000000000524AB524D000000000000231200000001D0000000000000000524";
String output = old.replace('D', 'C');
Upvotes: 0