以数字为分隔符分割字符串

从字符串返回元素直到识别出一个数字的最优雅方法是什么?


例子:


test213home --> test

234tower --> 

test --> test

还有另一个问题:


识别数字后从字符串返回元素的最优雅方法是什么?


例子:


test213home --> 213home

234tower --> 234tower

test --> 


慕侠2389804
浏览 333回答 3
3回答

繁花如伊

您不应以“优雅的方式”进行编码。您可以采用简单的解决方案(意味着易于编写/读取/维护(并非在所有情况下都尽可能快),也可以采用满足您的要求(大小,速度等)的解决方案。一个可能的简单解决方案(基于您的示例)可能是String[] strings = { "test213home", "234tower", "test", "foo42bar23"};for (String string : strings) {    String first = string.replaceFirst("^([^\\d]*).*", "$1");    String last = string.replaceFirst("^[^\\d]*(.*)", "$1");    System.out.printf("value: %-11s   first: %-4s   last: %s%n", string, first, last);}输出value: test213home   first: test   last: 213homevalue: 234tower      first:        last: 234towervalue: test          first: test   last: value: foo42bar23    first: foo    last: 42bar23对于正则表达式的解释,我建议您阅读有关“正则表达式”的Java教程。编辑在评论中找到您的问题的摘要。String[] strings1 = { "12345test123", "test123abc"};for (String string : strings1) {    String first = string.replaceFirst("^([^\\d]*[\\d]*).*", "$1");    String last = string.replaceFirst("^[^\\d]*[\\d]*(.*)", "$1");    System.out.printf("value: %-12s   first: %-7s   last: %s%n", string, first, last);}输出value: 12345test123   first: 12345     last: test123value: test123abc     first: test123   last: abc

宝慕林4294392

这是一种非正则表达式方法,在该方法中,您可以在字符串中搜索数字的索引(如果有的话)。然后,您只需得到substring结果即可。如果找不到数字,请注意三元选择。public class StackOverflow {&nbsp; &nbsp; public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; String[] data = { "test213home", "234tower", "test", "foo42bar23" };&nbsp; &nbsp; &nbsp; &nbsp; for (String d : data) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Before digit: " + beforeDigit(d));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("After digit : " + afterDigit(d));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; private static String beforeDigit(String data) {&nbsp; &nbsp; &nbsp; &nbsp; int index = findDigitIndex(data);&nbsp; &nbsp; &nbsp; &nbsp; return index > -1&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ? data.substring(0, index)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; : data.substring(0);&nbsp; &nbsp; }&nbsp; &nbsp; private static String afterDigit(String data) {&nbsp; &nbsp; &nbsp; &nbsp; int index = findDigitIndex(data);&nbsp; &nbsp; &nbsp; &nbsp; return index > -1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ? data.substring(index)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; : data.substring(0);&nbsp; &nbsp; }&nbsp; &nbsp; private static int findDigitIndex(String data) {&nbsp; &nbsp; &nbsp; &nbsp; int index = -1;&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < data.length(); i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (Character.isDigit(data.charAt(i))) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; index = i;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return index;&nbsp; &nbsp; }}结果:Before digit: testAfter digit : 213homeBefore digit:&nbsp;After digit : 234towerBefore digit: testAfter digit : testBefore digit: fooAfter digit : 42bar23
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java