-1

I have a given filename template stored in a String variable, for example: "fileno####save.txt"

I have a number between 1-9999 stored in an int variable.

If my number is 2, I'd like to get: "fileno0002save.txt"

If my number is 23, I'd like to get: "fileno0023save.txt"

If my number is 121, I'd like to get: "fileno0121save.txt"

If my number is 2021, I'd like to get: "fileno2021save.txt"

Let's see it with a little bit different template: "######fileno.txt"

If my number is 2, I'd like to get: "000002fileno.txt"

If my number is 23, I'd like to get: "000023fileno.txt"

If my number is 121, I'd like to get: "000121fileno.txt"

If my number is 2021, I'd like to get: "002021fileno.txt"

The length of the int number will never get any bigger than the length of the hashtags so we don't have to deal with that as I have already wrote an exception for that. The length of the hashtag part in the string that I have to deal with is between 2-10 characters.

sasieightynine
  • 372
  • 3
  • 13
  • Have you tried anything? Your algorithm should look something like this: 1. find the max number of consecutive hashtags (#) in a string {`MAX_HASH`} 2. left pad the number to match {`MAX_HASH`} length - {`PADDED_NUMBER`}. 3. replace {`MAX_HASH`} hashtags with {`PADDED_NUMBER`} – Plirkee Sep 24 '18 at 10:18
  • I already have the method for calculating the consecutive hashtags but I don't know how to do the padding, I can use only the standard java libraries. – sasieightynine Sep 24 '18 at 10:21
  • There are alraedy Q&A on padding https://stackoverflow.com/questions/4469717/left-padding-a-string-with-zeros – jschnasse Sep 24 '18 at 10:35

4 Answers4

1

For padding you could use something like this:

int maxHashes=10;
int myInt = 11;
String lpadPadStr = String.format("%1$" + maxHashes + "s", myInt).replace(' ', '0');
Plirkee
  • 3,426
  • 1
  • 11
  • 32
1

the below example is runnable; adapt it to your needs, for example by separating the 'read the template' and 'apply the template' steps.

import java.util.regex.*;

class Main {
    public static void main(String[] args) {
        Pattern HASHES = Pattern.compile("^(.*?)(#+)(.*)$");

        // read the template
        String pattern = "abc####foo.txt";

        Matcher m = HASHES.matcher(pattern);
        if (!m.matches()) throw new IllegalArgumentException("Invalid template");
        int len = m.group(2).length();
        String prefix = m.group(1);
        String suffix = m.group(3);

        // apply the template
        int input = 12;

        String f = prefix + String.format("%0" + len + "d", input) + suffix;
        System.out.println(f);
    }
} 
rzwitserloot
  • 44,252
  • 4
  • 27
  • 37
0

I think using a regular expression (RegEx) to find the arbitrary(?) number of hashtags would be the best option. You can split up the text by using groups and put in your number instead.

Ralfino
  • 39
  • 5
0

You can do something like this:

String format = "fileno####save.txt";
int num = 2;

int numberOfHash = format.length() - format.replace("#", "").length();

String hashes = format.substring(format.indexOf("#"), format.indexOf("#") + numberOfHash);

String paddedString = String.format(format.replace(hashes, "%0" + numberOfHash + "d"), num);

System.out.println(paddedString); // fileno0002save.txt
Mushif Ali Nawaz
  • 3,066
  • 3
  • 13
  • 26