从字母数字字符串列表创建唯一的文件名
我很抱歉创建了与现在许多类似的线程,但我主要是想对某些方法有一些了解。
我有一个字符串列表(可能只有 1 个或超过 1000 个) 格式= XXX-XXXXX-XX,其中每个字符串都是字母数字
我试图生成一个唯一的字符串(当前长度为18,但可能会更长,确保不会最大化文件长度或路径长度),如果我有相同的列表,我可以重现该字符串。顺序并不重要;尽管我可能会感兴趣是否也更容易限制订单。
我当前的Java代码如下(今天失败了,因此我在这里):
public String createOutputFileName(ArrayList alInput, EnumFPFunction efpf, boolean pHeaders) {
/* create file name based on input list */
String sFileName = "";
long partNum = 0;
for (String sGPN : alInput) {
sGPN = sGPN.replaceAll("-", ""); //remove dashes
partNum += Long.parseLong(sGPN, 36); //(base 36)
}
sFileName = Long.toString(partNum);
if (sFileName.length() > 19) {
sFileName.substring(0, 18); //Max length of 19
}
return alInput;
}
所以显然只是添加它们并没有很好地工作,我发现(也认为我应该采取最后18位而不是前18位)
有什么好的有没有可行的方法(可能与 CRC 相关)?
协助我创建密钥: 前 3 个字符几乎总是数字,并且可能有很多重复的字符(在 100 个字符中,可能只有 10 个不同的起始数字) 不允许使用这些字符 - I,O 最后两个字母字符子集中永远不会有一个字符然后是一个数字。
I apologize for creating a similar thread to many that are out there now, but I mainly wanted to also get some insight on some methods.
I have a list of Strings (could be just 1 or over a 1000)
Format = XXX-XXXXX-XX where each one is alphanumeric
I am trying to generate a unique string (currently 18 in length but probably could be longer ensuring not to maximize file length or path length) that I could reproduce if I have that same list. Order doesn't matter; although I may be interested if its easier to restrict the order as well.
My current Java code is follows (which failed today, hence why I am here):
public String createOutputFileName(ArrayList alInput, EnumFPFunction efpf, boolean pHeaders) {
/* create file name based on input list */
String sFileName = "";
long partNum = 0;
for (String sGPN : alInput) {
sGPN = sGPN.replaceAll("-", ""); //remove dashes
partNum += Long.parseLong(sGPN, 36); //(base 36)
}
sFileName = Long.toString(partNum);
if (sFileName.length() > 19) {
sFileName.substring(0, 18); //Max length of 19
}
return alInput;
}
So obviously just adding them did not work out so well I found out (also think I should take last 18 digits and not first 18)
Are there any good methods out there (possibly CRC related) that would work?
To assist with my key creation:
The first 3 characters are almost always numeric and would probably have many duplicate (out of 100, there may only be 10 different starting numbers)
These characters are not allowed - I,O
There will never be a character then a number in the last two alphachar subset.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我会使用系统时间。以下是您在 Java 中的操作方法:
如果您想添加一些有关商品及其部件号的信息,当然可以!
======== 编辑:“批处理对象是什么意思”=========
I would use the system time. Here's how you might do it in Java:
If you want to add some information about the items and their part numbers, you can, of course!
======== EDIT: "What do I mean by batch object" =========
除了 GlowCoder 的回应之外,我还想到了另一个可行的“不错的回应”。
我不会只在基数 36 中添加列表,而是会对同一个列表执行两件单独的事情。
在这种情况下,由于无法计算负数或小数,因此将每个数字相加并分别乘以每个数字并连接这些 base36 数字字符串也不是一个坏方法。
就我而言,我将取相加数字的最后九位数字和相乘数字的最后九位数字。这将消除我之前的错误并使其变得非常健壮。显然,一旦开始发生溢出,错误仍然可能存在,但在这种情况下也可以工作。延长允许的字符串长度也会使其更加健壮。
示例代码:
In addition to GlowCoder's response, I have thought of another "decent one" that would work.
Instead of just adding the list in base 36, I would do two separate things to the same list.
In this case, since there is no way for negative or decimal numbers, adding every number and multiplying every number separately and concatenating these base36 number strings isn't a bad way either.
In my case, I would take the last nine digits of the added number and last nine of the multiplied number. This would eliminate my previous errors and make it quite robust. It obviously is still possible for errors once overflow starts occurring, but could also work in this case. Extending the allowable string length would make it more robust as well.
Sample code: