2009-09-30 72 views
0

我只是遇到了一个问题(可能很简单)。但我无法弄清楚如何解决这个问题,也许你的某个人可以帮助我。Java字符串到Int

我接收输入的这个格式的字符串:

D0001001.tiff

,我需要回到下一个(假设下一个是一个要素接收incremente

。 。

输入:D0001001.tiff 输出:D0001002.tiff

没有零可错过我的方法是这样的(不带重构;))

private String getNextImageName(String last_image_name) 
{ 
    // Splits the name from the start to the . (not inclusive) 
    String next_name = last_image_name.substring(0, last_image_name.indexOf(".") - 1); 
    String next_extension = last_image_name.substring(last_image_name.indexOf(".") + 1, last_image_name.length() - 1); 

    String next_name_without_D = next_name.substring(1); 
    int next_name_withoud_D_value = Integer.parseInt(next_name_without_D); 

    // Increments to get the new name 
    next_name_withoud_D_value++; 

    String full_next_name = "D" + next_name_withoud_D_value + "." + next_extension; 

    return full_next_name; 
} 

但结果并不如预期:

输入:D0002001.tiff 输出:D201.tif

-

有一些约束,例如,数量0不能消失,因为最终的文件可以HACE不同的数字:

D0001001.tiff 或 D9999999001.tiff

但第二个进入只能通过999

D0001001.tiff 到 D0001999.tiff

到这一刻,我如此坚持,我也别想......

感谢

回答

2

如果将使用正则表达式,而不是使代码有点清洁:

private static final Pattern imgPattern = Pattern.compile("(.*)(\\d*)\\.(.*)"); 

public static String getNextImageName(String last) { 
    // The pattern captures the numerical value and the extension 
    Matcher matcher = imgPattern.matcher(last); 
    if (!matcher.matches()) { 
    throw new IllegalArgumentExecption("Not image pattern: " + last); 
    } 

    String prefix = matcher.group(1); 
    String num = matcher.group(2); 
    int numVal = Integer.value(num); 
    String ext = matcher.group(3); 

    return String.format("%s%0" + num.length() + "d.%s", 
       prefix, numVal + 1, ext); 
} 

一对夫妇的意见:

  1. 查找正则表达式规范理解模式和捕获。给定的模式基本上“返回”数值和扩展

  2. String.format()可以格式化数字并插入0作为垫。 String.format("%02d, 1)返回"01"String.format(%02d, 200)返回"200"

+4

抛出错误对于错误的输入看起来相当激烈。为什么不是IllegalArgumentException? – erickson 2009-09-30 16:58:30

+0

谢谢msaeed。它完美的作品。我甚至不懂代码。但是这个后期我会开始学习正则表达式。因为它看起来非常强大。 – Sheldon 2009-09-30 17:01:55

+0

与penpen的#1答案相比,可怕的代码。为什么人们跳到正则表达式来寻找这样的解决方案? – 2009-09-30 17:27:01

1
private static final Pattern p = Pattern.compile("D([0-9]+)(\\..+)"); 

private String getNextImageName(String previous) 
{ 
    Matcher m = p.matcher(previous); 
    if (!m.matches()) 
    throw new IllegalArgumentException("Invalid name: " + previous); 
    String number = m.group(2); 
    String ext = m.group(2); 
    String next = String.valueOf(Integer.parseInt(number) + 1); 
    int pad = Math.max(number.length() - next.length(), 0); 
    StringBuilder buf = new StringBuilder(1 + number.length() + ext.length()); 
    buf.append('D'); 
    while (pad-- > 0) 
    buf.append('0'); 
    buf.append(next); 
    buf.append(ext); 
    return buf.toString(): 
} 
3

字符串。格式可以为填充部分做的伎俩,例如:

System.out.println(String.format("D%07d", 10)); 

D0000010 
+1

号码的大小不固定。好的提示虽然 – notnoop 2009-09-30 17:51:34

0
public String getNextImageName(String s) { 
    int t = Integer.parseInt(s.substring(1,8)); 
    return new Formatter().format("D%07d.tiff", (t + 1) % 2000).toString(); 
} 

我不知道,如果你说的包裹在1999年的数字,如果他们不”吨,你应该拿出% 2000

+1

问题是,正如我所提到的,名称内的字符数量不固定。但是那个班非常有用。 – Sheldon 2009-09-30 17:06:48

1
import static org.apache.commons.lang.StringUtils.*; 
import static java.lang.Long.*; 
import static java.lang.String.*; 

... 

String fileName = "D0009999.tiff"; 

String numericPortion = substringBetween(fileName, "D", ".tiff"); 
int minimumNumberOfDigits = numericPortion.length(); 
long numericValue = parseLong(numericPortion); 

String nextFileName = format("D%0" + minimumNumberOfDigits + "ds.tiff", ++numericValue); 
+0

这是另一个优雅的解决方案。我明白了:D Cool SingleShot – Sheldon 2009-09-30 17:23:31

+0

很好的解决方案。完美解决这个问题。虽然不太健壮。 – notnoop 2009-09-30 17:53:20

+0

谢谢msaeed。 “不太健壮”是什么意思?缺乏处理数字格式的例外等等或其他什么?谢谢。 – SingleShot 2009-09-30 18:58:18