简体   繁体   English

Java用带零填充的整数替换字符串中的主题标签

[英]Java replace hashtags in a string with integer with padding zeros

I have a given filename template stored in a String variable, for example: "fileno####save.txt" 我有一个给定的文件名模板存储在String变量中,例如: "fileno####save.txt"

I have a number between 1-9999 stored in an int variable. 我将一个1-9999之间的数字存储在一个int变量中。

If my number is 2, I'd like to get: "fileno0002save.txt" 如果我的电话号码是2,我想获取: "fileno0002save.txt"

If my number is 23, I'd like to get: "fileno0023save.txt" 如果我的电话号码是23,我想获取: "fileno0023save.txt"

If my number is 121, I'd like to get: "fileno0121save.txt" 如果我的电话号码是121,我想获取: "fileno0121save.txt"

If my number is 2021, I'd like to get: "fileno2021save.txt" 如果我的电话号码是2021,请获取: "fileno2021save.txt"

Let's see it with a little bit different template: "######fileno.txt" 让我们用一些不同的模板来查看它: "######fileno.txt"

If my number is 2, I'd like to get: "000002fileno.txt" 如果我的电话号码是2,我想获取: "000002fileno.txt"

If my number is 23, I'd like to get: "000023fileno.txt" 如果我的电话号码是23,我想获取: "000023fileno.txt"

If my number is 121, I'd like to get: "000121fileno.txt" 如果我的电话号码是121,我想获取: "000121fileno.txt"

If my number is 2021, I'd like to get: "002021fileno.txt" 如果我的电话号码是2021,请获取: "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. int数字的长度永远不会超过标签的长度,因此我们不必处理它,因为我已经为此编写了一个例外。 The length of the hashtag part in the string that I have to deal with is between 2-10 characters. 我必须处理的字符串中的标签部分的长度在2到10个字符之间。

For padding you could use something like this: 对于填充,您可以使用以下内容:

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

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);
    }
} 

I think using a regular expression (RegEx) to find the arbitrary(?) number of hashtags would be the best option. 我认为使用正则表达式(RegEx)查找任意数量的#标签是最好的选择。 You can split up the text by using groups and put in your number instead. 您可以通过使用组来分割文本,然后输入数字。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM