1

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));
Community
  • 1
  • 1
janci
  • 49
  • 6
  • 1
    if stars are only present at the end of your `line`, you could calculate their number: `int starIndex = line.indexOf("*"); int numberOfStars = line.length() - line.substring(starIndex).length();` the method `indexOf(String str)` returns the first occurence of a character/string in your string. – peech Mar 02 '17 at 15:58

3 Answers3

1

First try to pre-fill a String with '0', add your magic number at the end. Then simply substring will work since you know how long your '*' are and where they start.

This also works:

    String s1 = "someThing/one_text_0000_temperature****";
    String step = "94";
    String v = "0000000000" + step;
    String result = s1.substring(0, s1.indexOf('*')) + v.substring(v.length() - s1.length() - s1.indexOf('*'));
    System.out.println(result);
Renats Stozkovs
  • 2,549
  • 10
  • 22
  • 26
0

i try this :

String test = "test****";  //test: "test****"
int one = test.indexOf("*"); //one: 4
int two = test.lastIndexOf("*"); //two: 7
int nb = two-one; //nb: 3 two: 7 one: 4

String newTest= test.replace("*","0"); //newTest: "test0000"
String step = "12"; //step: "12"
newTest = newTest.substring(0,newTest.length()-step.length()); //newTest: "test00"
newTest += step; //newTest: "test001"

You can also add a size check between 'nb' and 'step.length()' . if step.length() is is taller than your number or '*' what do you do ?

marchat
  • 153
  • 1
  • 9
0

I wrote this code for your problem :

    int step = 94;
    String[] input = new String[]{
        "someThing/one_text_0000_temperature****",
        "another/two_text_0000_temperature****",
        "where/three_text_0000_temperature****"
    };

    for (String i : input) {
        int start = i.indexOf('*');
        int size = i.length() - start;
        int stepsize = (step + "").length();
        if(stepsize > size)  {
            throw new IllegalArgumentException("Too big step :D");
        }
        String result = i.replace('*', '0').substring(0, i.length() - stepsize) + step;

        System.out.println(result);
    }

I hope it's help you :)

Valentin Michalak
  • 2,089
  • 1
  • 14
  • 27