-3

我的数据库表中有一些字符串类型的数据,例如 asD-101D-102. 我想在前端(JSF Web 应用程序)中自动获取下一个数据以发送到数据库,即D-103.

为此,我只想从字符串中获取 int 数据。

我怎样才能做到这一点?

4

3 回答 3

3

在您的托管 bean 中,您可以使用split()函数。

String s = "D-101";
String[] arr = s.split("[^\\d]+");
System.out.println(arr[1]); //prints 101

或者

在 xhtml 页面中,您可以编写这样的 EL。请注意,myBean是您的 bean 的名称,getColumnValue()方法返回一个列的值(即“D-101”)。

#{myBean.columnValue.split('[^\\d]+')[1]}
于 2012-11-20T13:09:31.437 回答
3
final Pattern lastIntPattern = Pattern.compile("[^0-9]+([0-9]+)$");
            String input = D101;
            Matcher matcher = lastIntPattern.matcher(input);
            if (matcher.find()) {
                String someNumberStr = matcher.group(1);
                int lastNumberInt = Integer.parseInt(someNumberStr);
                System.out.println("Test int output - " + lastNumberInt);
                inputList.add(lastNumberInt);
            }

然后比较最大值

int currentBranchCode = 0;
        for (Integer CCS : companyArrayList) {
            if (currentBranchCode <= CCS) {
                currentBranchCode = CCS;
            }
        }

进而

currentBranchCode =currentBranchCode +1;
String codegenerate="D"+currentBranchCode;

你必须尝试一下。

于 2012-12-01T08:06:40.570 回答
1

这可以通过几行来完成:

int i = Intger.parseInt(input.replaceAll(".*(?<!\\d)(\\d+)", "$1");
String next = input.replaceAll("(.*)(?<!\\d)\\d+", "$1"+ ++i);
于 2012-12-01T08:26:53.197 回答