Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何读取和删除字符串中的数字?_Java_String - Fatal编程技术网

Java 如何读取和删除字符串中的数字?

Java 如何读取和删除字符串中的数字?,java,string,Java,String,例如,我有一个字符串: 0no1no2yes3yes4yes 此处的第一个0应被删除并用作数组的索引。我这样做是通过以下声明: string = string.replaceFirst(dataLine.substring(0, 1), ""); 但是,当我说这个字符串时: 10是11否12是13是14否 我的代码失败,因为我想处理10,但我的代码只提取1 因此,在排序中,一位数可以正常工作,但两位数或三位数会导致indexootfound错误 代码如下: 下面是一些示例数据: 以下是示例数据

例如,我有一个字符串:

0no1no2yes3yes4yes

此处的第一个0应被删除并用作数组的索引。我这样做是通过以下声明:

string = string.replaceFirst(dataLine.substring(0, 1), "");
但是,当我说这个字符串时:

10是11否12是13是14否

我的代码失败,因为我想处理
10
,但我的代码只提取
1

因此,在排序中,一位数可以正常工作,但两位数或三位数会导致
indexootfound
错误

代码如下:

下面是一些示例数据:

以下是示例数据的输出:

Enter filename: test.txt
Data before cleanUp: {"assignmentID":"2CCYEPLSP75KTVG8PTFALQES19DXRA","workerID":"AGMJL8K9OMU64","start":1359575990087,"end":"","elapsedTime":"","itemIndex":0,"responses":[{"jokeIndex":0,"response":"no"},{"jokeIndex":1,"response":"no"},{"jokeIndex":2,"response":"yes"},{"jokeIndex":3,"response":"yes"},{"jokeIndex":4,"response":"yes"}],"mturk":"yes"},
Data after cleanUp: 0no1no2yes3yes4yes
Data before cleanUp: {"assignmentID":"2118D8J3VE7W013Z4273QCKAGJOYID","workerID":"A2P0GYVEKGM8HF","start":1359576154789,"end":"","elapsedTime":"","itemIndex":3,"responses":[{"jokeIndex":15,"response":"no"},{"jokeIndex":16,"response":"no"},{"jokeIndex":17,"response":"no"},{"jokeIndex":18,"response":"no"},{"jokeIndex":19,"response":"no"}],"mturk":"yes"},
Data after cleanUp: 15no16no17no18no19no
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 2
    at java.lang.String.substring(String.java:1907)
    at jokes.main(jokes.java:34)
基本上,代码应该做的是将数据剥离成如上所示的字符串,然后读取数字,如果后跟
yes
则在
dataYes
中增加其索引值,或者如果后跟
no
则在
dataNo
中增加值。有道理吗

我能做什么?如何使我的代码更灵活?

对您有用吗

string = string.replaceAll("^\\d+","");
那么:-

String regex = "^\\d+";
String myStr = "10abc11def";

Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(myStr);

if(m.find())
{
    String digits = m.group();
    myStr = m.replaceFirst("");
}
试试这个

    System.out.println("10yes11no12yes13yes14no".replaceFirst("^\\d+",""));

另一种更具体的尝试:-

    String regex = "^(\\d+)(yes|no)";
    String myStr = "10yes11no";

    Pattern p = Pattern.compile(regex);
    Matcher m = p.matcher(myStr);

    while (m.find())
    {
        String all = m.group();
        String digits = m.group(1);
        String bool = m.group(2);

        // do not try and combine the next 2 lines ... it doesn't work!
        myStr = myStr.substring(all.length());
        m.reset(myStr);

        System.out.println(String.format("all = %s, digits = %s, bool = %s", all, digits, bool));
    }

你试过正则表达式吗?我不熟悉java,但似乎可以用正则表达式来完成。是否需要删除字符串中的每个数字?我不太明白您想要什么。你能在这里发布一些输入和输出示例吗?我无法打开那个链接。你没有很清楚地解释你想要发生什么。请尝试提供有关如何更改输入的详细信息。@IDONMARCII:在使用正则表达式检查后,如何使用数字?据我所知,这将删除所有非数字。但我想分析数字和非数字。@user2027425根据我的理解,你的理解是错误的<代码>^\\d+表示所有前导数字<代码>^是行的开头。这段代码删除了所有的前导数字。我想这只会删除它们。。。OP不想得到数字并使用它们吗?@PeterJamieson是正确的。我想用这个号码!是否有一种方法可以提取前导数字,并在将其转换为
int
后将其存储在变量中?如果要重新使用匹配器获取后续数字,也可以从正则表达式中删除^即使在导入
util.regex.Pattern
util.regex.Matcher
之后也找不到