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.
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,
regex inside parenthesis (...) 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
so (.{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)
in replacement "$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)
Also assure match could be done only every 50 characters from start of the string I will add \\G represents start of the string or previously match (so there can't be any characters between previous match and current match).
Just use java's String replace method.
String old = "AB524D000000000000231200000001D0000000000000000524AB524D000000000000231200000001D0000000000000000524AB524D000000000000231200000001D0000000000000000524AB524D000000000000231200000001D0000000000000000524AB524D000000000000231200000001D0000000000000000524";
String output = old.replace('D', 'C');
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 ...
}
Related
For example, I would like to convert the int value 12 into a String output of BCD: 00 12 (0x00 0x12).
If I have int value of 256, it will be 02 56 (which is 0x02 0x56),
or if I have a int value of 999, it will be 09 99 (0x09 0x99),
9999 would be 99 99 (0x99 0x99).
Right now, my only solution is to create a String array of size 4, and calculate how many characters are there by converting the int value into String. If there are 2 characters, I will add 2 x 0 into the array first before adding the 2 characters, and then make them back into a single String variable.
Basically,
int value = 12;
String output = Integer.toString(value);
// then count the number of characters in the String.
// 4 minus (whatever number of characters in the String, add zeros
// add the characters:
stringArray[0] = "0";
stringArray[1] = "0";
stringArray[2] = "1";
stringArray[3] = "2";
// then, concatenate them back
If there are 3 characters, I will add one 0 into the array first before adding 3 characters. I was wondering if there is any other way?
You can use String.format to append leading 0 and use substring to split in to two part.
int value = 12;
String output = String.format("%04d",value);
System.out.println(output.substring(0,2)+" "+output.substring(2,4));
String.format("%04d",value) will append 0s in the front if the length is less than 4.
If you do not want to use substring you can use String.split and String.join like below.
System.out.println(
String.join(
" ",
Arrays.asList(
output.split("(?<=\\G.{2})")
)
)
);
output.split("(?<=\\G.{2})") will split the string in 2 characters each.
Is that what you are asking for?
public static String formatTheString(String string, int length) {
return String.format("%"+length+"s", string).replace(' ', '0');
}
and pass the values like
formatTheString(Integer.toString(256),4);
I think what you are asking is not correct.
refer this for BCD.
and below code is sufficient for what you need
System.out.printf("%04d",n);
in above code n is your number.
There is e.x 2.52549856E8 float number.
What I want is simply make it 25.2549856E8, that's it, everything else can stay.
I sought for solution and all I found was with bunch of string examples.
I get pure float number. Should I convert it to String first? Is there a simpler way to do this?
if you want to just move the dot one digit to the right, you can use following snippet, which uses String#substring() to cut the String into the right parts and then concatenates them again:
String number = String.valueOf(2.52549856E8f);
int index = number.indexOf('.');
String formatted =
number.substring(0, index) +
number.substring(index + 1, index + 2) +
'.' +
number.substring(index + 2);
number.substring(0, index) cuts the first digit out
number.substring(index + 1, index + 2) cuts the second digit out
'.' inserts the new dot
number.substring(index + 2) appends the rest of the number
The same can be done with regex:
String number = String.valueOf(2.52549856E8f);
String formatted = number.replaceAll("^(\\d)\\.(\\d)(\\d+E\\d+)$", "$1$2.$3");
This question is similar to my previous question Split a string contain dash and minus sign. But I asked it in a wrong and then it got a slightly different semantics and people answered(including) in that perspective. Therefore rather than modifying that question I thought it's better to ask in a new question.
I have to split a string which contain hyphen-minus character and minus sign. I tried to split based on the unicode character (https://en.wikipedia.org/wiki/Hyphen#Unicode), still it considering minus sign same as hyphen-minus character. Is there a way I can solve it?
Expected output
(coun)
(US)
-1
Actual output
(coun)
(US)
// actually blank line will print here but SO editor squeezing the blank line
1
public static void main(String[] args) {
char dash = '-';
int i = -1;
String a = "(country)" + dash + "(US)" + dash + i;
Pattern p = Pattern.compile("-", Pattern.LITERAL);
String[] m = p.split(a);
for (String s : m) {
System.out.println(s);
}
}
char dash = '\u2010'; // 2010 is hyphen, 002D is hyphen-minus
int i = -1;
String a = "(country)" + dash + "(US)" + dash + i;
Pattern p = Pattern.compile("\u2010", Pattern.LITERAL);
String[] m = p.split(a);
for (String s : m) {
System.out.println(s);
}
The string representation of an integer always uses the hyphen-minus as the negative sign:
From Integer.toString:
If the first argument is negative, the first element of the result is the ASCII minus character '-' ('\u002D'). If the first argument is not negative, no sign character appears in the result.
so in the end your string has 3 hyphen-minus characters. That's why split can't distinguish between them.
Since you can't change the string representation of an integer, you need to change the dash variable to store a hyphen instead of hyphen-minus. Now there are 2 hyphens and 1 hyphen-minus in your string, making split able to distinguish between them.
Actually I'm trying to replace number to words in the sentence that giving by user. This case date format; For example: My birthday is on 16/6/2000 and I'm newbie to the java --> become ---> My birthday is on sixteenth july two thousand and I'm newbie to the java
Here is code:
Scanner reader = new Scanner(System.in);
System.out.println("Enter any numbers: ");
String nom = reader.nextLine(); // get input from user
//checking contains that has "/" or not
if(nom.contains("/")){
String parts[] = nom.split("[/]");
String part1 = parts[0]; //data before "/" will be stored in the first array
String day[] = part1.split("\\s+");// split between space
String get_day = day[day.length -1];// get last array
String get_month = parts[1]; //data in the between of "/" will be stored in the second array
String part3 = parts[2]; // data after "/" will be stored in the third array
String year[] = part3.split("\\s+");// split between space
String get_year = year[0];// get first array
String s = NumberConvert.convert(Integer.parseInt(get_day)) +
NumberConvert.convert(Integer.parseInt(get_month)) +
NumberConvert.convert(Integer.parseInt(get_year));
String con = nom.replaceAll("[0-9].*/[0-9].*/[0-].*", s); // replace number to word
System.out.println(con); // print the data already converted
} else {....}
But the result that I have got is:
My birthday is on sixteenth july two thousand
//"and I'm newbie to the java" is disappear [How to solve it]//
How to solve it. Actually I want to get value before and after of "/" slash and convert it to words and replace it as a original input from user.
What I have tried is:
String con = nom.replaceAll("[0-9].*/[0-9].*/[0-9999]", s); // a bit change [0-9].* to [0-9999]
But output become like this:
My birthday is on sixteenth july two thousand 000 and I'm newbie to the java
//after two thousand index "000" is appearing
The regex is wrong:
[0-9].*/[0-9].*/[0-].*
What it means:
[0-9] match a single number in the range between 0 and 9
.* matches any character (except newline) between zero and unlimited times, as many times as possible, giving back as needed [greedy]
/ matches the character / literally
[0-9] match a single number in the range between 0 and 9
.* matches any character (except newline) between zero and unlimited times, as many times as possible, giving back as needed [greedy]
/ matches the character / literally
[0-] match a single number in the list 0- literally
.* matches any character (except newline) between zero and unlimited times, as many times as possible, giving back as needed [greedy]
It should be:
[0-9][0-9]/[0-9][0-9]/[0-9][0-9][0-9][0-9]
Or, better:
\d{2}/\d{2}/\d{4}
You can also use below regex pattern to get all the numbers from String:
String st = "My birthday is on 16/6/2000 and I'm newbie to the java, using since 2015";
Pattern p = Pattern.compile("-?\\d+");
Matcher m = p.matcher(st);
while (m.find()) {
System.out.println(m.group());
}
I am rather new at java, and I know this cant be too difficult, but I cant quite figure it out.
String xxChange = request.getParameter("XXChange");
String yyChange = request.getParameter("YYChange");
String zzChange = request.getParameter("ZZChange");
String aaaChange = request.getParameter("AAChange");
So basically I am getting these parameters and just setting them to strings. Each one of these strings has multiple numbers then two letters after it, and my question is how do I remove the letters and set it to a new string. Fyi....the number in front of the letters is on a sequence, and could grow from being 1 digit to multiple, so i dont think i can do string.Substring.
You want to remove the last two characters, so use substring like this:
String s = "123AB";
String numbers = s.substring(0, s.length() - 2);
You could test something like
String value = "123ABC";
System.out.println(value.replaceAll("\\d+(.*)", "$1"));
If you only need the last two letters:
String input = "321Ab";
String lastTwo = input.substring(input.length() - 2);
If you need both the digits and the letters use a regular expression:
Pattern p = Pattern.compile("(\\d+)(\\w{2})");
Matcher m = p.matcher("321Ab");
if (m.matches()) {
int number = Integer.parseInt(m.group(1)); // 321
String letters = m.group(2); // Ab
...
}