2016-02-25 208 views
0
Caused by: com.some.packge.MyCustomException: blah, blah, blah 

我怎样才能得到上面的字符串的子串以这样的方式,我得到的只是等等等等一部分之后开始。 我在下面尝试,但我得到MyCustomException以及我的子字符串。获取子串给定的字符串

mainString.substring(mainString.indexOf("MyCustomException")); 
+4

你真的在使用异常类吗?如果是这样的话,您可以使用一些内置函数来获取该文本。 exception.getMessage()或类似的东西。 – Sh4d0wsPlyr

回答

6

你可以做这样的事情:

mainString = mainString.substring(mainString.indexOf("MyCustomException:") + "MyCustomException:".length()); 

你应该检查String包含MyCustomException:你这样做之前。

2

使用lastIndexOf()。事实上,我现在不好纠正

mainString.substring(mainString.lastIndexOf("MyCustomException:") + "MyCustomException:".length()); 
相关问题