I have array of string which looks like this:
someThing/one_text_0000_temperature****
another/two_text_0000_temperature****
where/three_text_0000_temperature****
I have variable step.
int step
I need to replace those **** with number in variable step.
Example of output if step is 94:
someThing/one_text_0000_temperature0094
another/two_text_0000_temperature0094
where/three_text_0000_temperature0094
Problem is that number of * is changing. Well, when program is running it is constant and same for each string. But those strings are from file. Number of * can be different in next start of program, file has changed.
I thinking that I will do that in 3 steps: find number of stars, format step into string, and finaly replace part of string with new string
question 1) how to find out number of stars?
question 2) how to format step variable into dynamic length string? Not like this:
String.format("%04d", step); // how to change that 4 if needed?
question 3)
how to replace part of string with another string
This could be done by calling replace. Not sure if line = line.replace( ) is efficient / correct?
String line = new String("someThing/one_text_0000_temperature****");
String stars = new String("****"); // as result of step 1
String stepString = new String("0094"); // as result of step 2
line = line.replace(stars, stepString);
thank you very much for tips / help
Edited
Thank you for inspiration. I did find some more ideas here Simple way to repeat a String in java and my final code:
int kolko = line.length() - line.indexOf("*");
String stars = String.format("%0"+kolko+"d", 0).replace("0", "*");
String stepString = String.format("%0"+kolko+"d", step);
I have lines stored in HashMap so I can used lambda
lines.replaceAll((k, v) -> v.replace(stars, stepString));