Java 子字符串:“字符串索引超出范围”
我想我收到此错误是因为该字符串试图对 null
值进行子串。 但是 ".length() > 0"
部分不会消除这个问题吗?
这是 Java 片段:
if (itemdescription.length() > 0) {
pstmt2.setString(3, itemdescription.substring(0,38));
} else {
pstmt2.setString(3, "_");
}
我收到此错误:
java.lang.StringIndexOutOfBoundsException: String index out of range: 38
at java.lang.String.substring(Unknown Source)
at MASInsert2.itemimport(MASInsert2.java:192)
at MASInsert2.processRequest(MASInsert2.java:125)
at MASInsert2.doGet(MASInsert2.java:219)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:627)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:269)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:172)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:117)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:108)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:174)
at org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:835)
at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:640)
at org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1286)
at java.lang.Thread.run(Unknown Source)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
遗憾的是,
substring
没有以处理短字符串的方式实现——就像其他语言(例如 Python)一样。好吧,我们无法改变这一点,并且每次使用 substr 时都必须考虑这种边缘情况,我会选择这个较短的变体,而不是 if-else 子句:
It is a pity that
substring
is not implemented in a way that handles short strings – like in other languages e.g. Python.Ok, we cannot change that and have to consider this edge case every time we use
substr
, instead of if-else clauses I would go for this shorter variant:不,当 itemdescription 为 null 时调用 itemdescription.length() 不会生成 StringIndexOutOfBoundsException,而是生成 NullPointerException,因为您实际上是在尝试调用 null 上的方法。
正如其他人所指出的,StringIndexOutOfBoundsException 指示 itemdescription 的长度不至少为 38 个字符。 您可能想处理这两个条件(我假设您想截断):
如果您经常这样做,可能是实用函数的好地方......
No, calling itemdescription.length() when itemdescription is null would not generate a StringIndexOutOfBoundsException, but rather a NullPointerException since you would essentially be trying to call a method on null.
As others have indicated, StringIndexOutOfBoundsException indicates that itemdescription is not at least 38 characters long. You probably want to handle both conditions (I assuming you want to truncate):
Might be a good place for a utility function if you do that a lot...
我会推荐 apache commons lang。 一句话就能解决这个问题。
I would recommend apache commons lang. A one-liner takes care of the problem.
当您尝试获取从比字符串长的索引开始的子字符串时,Java 的
substring
方法会失败。一个简单的替代方法是使用 Apache Commons
StringUtils.substring
:注意,如果您由于某种原因无法使用 Apache Commons lib,您可以从来源
Java's
substring
method fails when you try and get a substring starting at an index which is longer than the string.An easy alternative is to use Apache Commons
StringUtils.substring
:Note, if you can't use Apache Commons lib for some reason, you could just grab the parts you need from the source
您确实需要检查字符串的长度是否大于或等于 38。
You really need to check if the string's length is greater to or equal to 38.
substring(0,38)
表示字符串必须为 38 个字符或更长。 如果不是,则“字符串索引超出范围”。substring(0,38)
means the String has to be 38 characters or longer. If not, the "String index is out of range".我假设您的列长度为 38 个字符,因此您需要截断
itemdescription
以适合数据库。 像下面这样的实用函数应该可以完成您想要的操作:然后您只需像这样调用它:
I'm assuming your column is 38 characters in length, so you want to truncate
itemdescription
to fit within the database. A utility function like the following should do what you want:then you just call it like so:
itemdescription
少于 38 个字符。 这就是抛出StringOutOfBoundsException
的原因。检查
.length() > 0
只是确保String
有一些非空值,您需要做的是检查长度是否足够长。 你可以尝试:itemdescription
is shorter than 38 chars. Which is why theStringOutOfBoundsException
is being thrown.Checking
.length() > 0
simply makes sure theString
has some not-null value, what you need to do is check that the length is long enough. You could try:您必须检查字符串长度。 您假设只要 String 不为
null
就可以执行substring(0,38)
,但实际上您需要 String 的长度至少为 38 个字符。You must check the String length. You assume that you can do
substring(0,38)
as long as String is notnull
, but you actually need the String to be of at least 38 characters length.当合适时,我使用匹配而不是子字符串。
使用子字符串:
使用匹配(必须使用正则表达式表示法):
When this is appropriate, I use matches instead of substring.
With substring:
With matches (must use Regex notation):
如果有人面临同样的问题。
做这个:
str.substring (...(trim()) ;
希望它对某人有帮助
Should anyone face the same problem.
Do this:
str.substring (...(trim()) ;
Hope it helps somebodies ????
如果 itemdescription 少于 38 个字符,您会得到此信息
您可以在 JAVA API 中查看抛出哪些异常以及何时抛出
在您的情况下 String#substring(int,int): https://docs.oracle.com/javase/9/docs/api/java/lang/String.html#substring-int-int-
You get this if itemdescription is shorter than 38 characters
You can look which exceptions are thrown and when in the JAVA API
in you case for String#substring(int,int): https://docs.oracle.com/javase/9/docs/api/java/lang/String.html#substring-int-int-