简体   繁体   English

如何在Java中使用正则表达式或子字符串从字符串中提取文本?

[英]How can I extract text from a string using regex or substring in java?

I have this Runnable 我有这个Runnable

Runnable serverChecksRunnable = new Runnable()
    {
        @Override
        public void run()
        {
            if (connectedSuccess == true)
            {
                    checkServer = Get(iptouse + "uploadstatus");

            }

            Handler h=new Handler(Looper.getMainLooper());
            h.post(new Runnable()
            {
                @Override
                public void run()
                {
                    if (connectedSuccess)
                    {
                        if (checkServer != null)
                        {
                            String a = null;
                            try
                            {
                                a = new String(checkServer, "UTF-8");
                                textforthespeacch = a;
                                {
                                    String varr = textforthespeacch.substring(17,3);
                                    String varr1 = textforthespeacch.substring(0, 16);
                                    String varr2 = textforthespeacch.substring(21);
                                    textforthespeacch = varr1;
                                    status1.setText("Upload completed" + " " + varr + "%");
                                    timerValue.setText("00:00:" + varr2);
                                    numberofuploadedfilescounter += 1;
                                    uploadedfilescount.setText(("Uploaded Files: " + numberofuploadedfilescounter));
                                    MainActivity.this.initTTS();
                                }
                                if (textforthespeacch.contains("uploading"))
                                {
                                    String[] split = textforthespeacch.split(" ");
                                    textforthespeacch = split[0];
                                    status1.setText("Uploading" + " " + split[1] + "%");
                                    servercheckCounter += 1;
                                    if (servercheckCounter == 1)
                                    {
                                        MainActivity.this.initTTS();
                                    }
                                }
                            } catch (UnsupportedEncodingException e)
                            {
                                e.printStackTrace();
                            }
                        }
                    }

                }
            });

            customHandler.postDelayed(serverChecksRunnable,1000);
        }
    };

The var textforthespeacch is string and get text from a web server. var textforthespeacch是字符串,可以从Web服务器获取文本。 Then i check when it contains a part of a text assign the text parts to three vars: 然后我检查它是否包含文本的一部分,将文本部分分配给三个变量:

if (textforthespeacch.contains("upload completed"))
                                {
                                    String varr = textforthespeacch.substring(17,3);
                                    String varr1 = textforthespeacch.substring(0, 16);
                                    String varr2 = textforthespeacch.substring(21);

The problem is that the program crash on the first substring(17,3) 问题在于程序在第一个子字符串上崩溃(17,3)

The text in the textforspeech is: "upload completed 100 0" The number 100 present percentages and will be all the time 100. And the number 0 present seconds it can be changed sometimes 0 sometimes 1 and sometimes 34.5 textforspeech中的文本为:“上载完成100 0”数字100表示​​百分比,并且始终为100。数字0表示秒,可以更改,有时0有时1有时34.5

In varr I need to put the 100 as string "100" In varr1 "upload completed" in varr2 "0" 在varr中,我需要将100作为字符串“ 100”放置在varr1中“ upload complete”在varr2“ 0”中

varr2 might be changed to can be "0" or "1" or "45.5" the others varr and varr1 never change the text "100" and "upload completed" never change. varr2可能会更改为可以为“ 0”,“ 1”或“ 45.5”,其他varr和varr1永远不会更改文本“ 100”,而“上载完成”也不会更改。

This is the exception message in the logcat i'm getting: 这是我收到的logcat中的异常消息:

09-15 18:45:45.435    5583-5583/com.test.webservertest E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.test.webservertest, PID: 5583
    java.lang.StringIndexOutOfBoundsException: length=22; regionStart=17; regionLength=-15
            at java.lang.String.startEndAndLength(String.java:504)
            at java.lang.String.substring(String.java:1333)
            at com.adilipman.webservertest.MainActivity$2$1.run(MainActivity.java:235)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5274)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:909)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:704)

With this line 用这条线

String varr = textforthespeacch.substring(17,2);

The first value in a substring call must be less than the second. substring调用中的第一个值必须小于第二个。 That is why you get regionLength=-15 . 这就是为什么得到regionLength=-15

Here is the syntax for the substring() method: 这是substring()方法的语法:

public String substring(int beginIndex)

or 要么

public String substring(int beginIndex, int endIndex)

where 哪里

beginIndex -- the begin index, inclusive.

endIndex -- the end index, exclusive.

How can the endIndex in: String varr = textforthespeacch.substring(17,3); endIndex如何在: String varr = textforthespeacch.substring(17,3); be less than the beginIndex . 小于beginIndex

I think it should be the other way round. 我认为应该相反。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM