如何在java中使用正则表达式或子字符串从字符串中提取文本?
我有这本书如何在java中使用正则表达式或子字符串从字符串中提取文本?,java,Java,我有这本书 Runnable serverChecksRunnable = new Runnable() { @Override public void run() { if (connectedSuccess == true) { checkServer = Get(iptouse + "uploadstatus"); }
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);
}
};
var textforthespeacch是字符串,用于从web服务器获取文本。
然后我检查它何时包含文本的一部分,并将文本部分分配给三个变量:
if (textforthespeacch.contains("upload completed"))
{
String varr = textforthespeacch.substring(17,3);
String varr1 = textforthespeacch.substring(0, 16);
String varr2 = textforthespeacch.substring(21);
问题是程序在第一个子串(17,3)上崩溃
textforspeech中的文本是:“上传完成100 0”数字100当前百分比,并且始终为100。现在的秒数,有时是0,有时是1,有时是34.5
在varr中,我需要将100作为字符串“100”
在varr1中“上传完成”
在varr2“0”中
varr2可能更改为可以是“0”或“1”或“45.5”。其他varr和varr1从不更改文本“100”和“上载完成”从不更改
这是我收到的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)
用这条线
String varr = textforthespeacch.substring(17,2);
子字符串中的第一个值必须小于第二个值。这就是为什么会得到regionLength=-15
以下是substring()
方法的语法:
public String substring(int beginIndex)
或
在哪里
如何在:String varr=text中为peacch.substring(17,3)设置endIndex
代码>必须小于beginIndex
我认为应该是另一种情况。textforthespeacch.substring(17,3)代码>。这应该是peacch.子字符串(3,17)的text
hay Daniel,只发布代码的相关部分,这总是很容易理解和吸引人的,但是:if(connectedSuccess==true)
会引发问题,比如if(connectedSuccess=true)
(=
是赋值,而不是比较),所以你应该避免这种风格。最好使用if(connectedSuccess)
(或if(!connectedSuccess)
而不是==false
)。如果(true==connectedSuccess)
,您也可以尝试yoda风格的,这是安全的,因为无法为true
literal.Emd4600分配任何值。字符串是:“upload completed 100 0”,当我在varr10中执行时,16我得到了“upload completed”部分,这很好。现在在varr中,我只需要得到“100”,在varr2中,我只需要得到“0”,而“0”可能是下一次的“1”或“45.5”。在varr中执行3,17给我“oad completed”,我需要在varr中执行“100”,然后您要做的是string.substring(17,string-length()-2)代码>(注意可能是-3,我不确定)
public String substring(int beginIndex, int endIndex)
beginIndex -- the begin index, inclusive.
endIndex -- the end index, exclusive.