在索引处分割字符串
问题描述:
如何在特定索引处拆分字符串?例如在索引10处分割字符串,使该字符串现在等于索引10之前的所有内容,然后转储其余部分.
How would I split a string at a particular index? e.g split string at index 10, making the string now equal to everything up to index 10 and then dumping the remainder.
答
substring(0,10)
或substring(0,11)
取决于索引10是否应包含10?不过,您必须检查length() >= index
.
What about substring(0,10)
or substring(0,11)
depending on whether index 10 should inclusive or not? You'd have to check for length() >= index
though.
另一种选择是org.apache.commons.lang.StringUtils.substring("your string", 0, 10);