我猜我得到了这个错误,因为字符串试图对一个null值进行子串.但这".length() > 0"部分不会消除这个问题吗?
这是Java片段:
if (itemdescription.length() > 0) {
pstmt2.setString(3, itemdescription.substring(0,38));
}
else {
pstmt2.setString(3, "_");
}
Run Code Online (Sandbox Code Playgroud)
我收到了这个错误:
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)
Run Code Online (Sandbox Code Playgroud) 在Python中查找另一个字符串中的字符串的优雅方法是什么,但仅当子字符串在整个单词内,而不是单词的一部分时?
也许一个例子将证明我的意思:
string1 = "ADDLESHAW GODDARD"
string2 = "ADDLESHAW GODDARD LLP"
assert string_found(string1, string2) # this is True
string1 = "ADVANCE"
string2 = "ADVANCED BUSINESS EQUIPMENT LTD"
assert not string_found(string1, string2) # this should be False
Run Code Online (Sandbox Code Playgroud)
我怎样才能最好地编写一个名为string_found的函数来完成我需要的工作呢?我想也许我可以用这样的东西来捏造它:
def string_found(string1, string2):
if string2.find(string1 + " "):
return True
return False
Run Code Online (Sandbox Code Playgroud)
但这并不是很优雅,如果它在string2的末尾也不会匹配string1.也许我需要正则表达式?(argh regex fear)
我在大多数语言中很容易解决问题,但我似乎无法批量解决.我想提取字符串的最后一个字符.在伪代码..
if var1.substring(var1.length, -1) = "0"
do something
Run Code Online (Sandbox Code Playgroud)
在英语中...如果字符串中的最后一个字符是0则...
我有一个这样的列表,我希望能够在此列表中搜索来自另一个字符串的子字符串.例:
List<string> list = new List<string>();
string srch = "There";
list.Add("1234 - Hello");
list.Add("4234 - There");
list.Add("2342 - World");
Run Code Online (Sandbox Code Playgroud)
我想"There"在我的列表中搜索并返回"4234 - There".我试过了:
var mySearch = list.FindAll(S => s.substring(srch));
foreach(var temp in mySearch)
{
string result = temp;
}
Run Code Online (Sandbox Code Playgroud) 如果元素与子字符串匹配,如何从列表中删除元素?
我尝试使用pop()和enumerate方法从列表中删除元素,但似乎我缺少一些需要删除的连续项:
sents = ['@$\tthis sentences needs to be removed', 'this doesnt',
'@$\tthis sentences also needs to be removed',
'@$\tthis sentences must be removed', 'this shouldnt',
'# this needs to be removed', 'this isnt',
'# this must', 'this musnt']
for i, j in enumerate(sents):
if j[0:3] == "@$\t":
sents.pop(i)
continue
if j[0] == "#":
sents.pop(i)
for i in sents:
print i
Run Code Online (Sandbox Code Playgroud)
输出:
this doesnt
@$ this sentences must be removed
this shouldnt
this isnt
#this should …Run Code Online (Sandbox Code Playgroud) 可以将否定的一个用作Java中string.substring的结束索引吗?
例:
String str = "test";
str.substring(0, str.indexOf("q"));
Run Code Online (Sandbox Code Playgroud)
编辑:在javadocs中没有任何地方直接说endindex不能为负.在其他语言和库中存在子串的实现,其允许负的endindex但是不允许负的beginindex,因此这显然是相关的.它也没有任何暗示.(编辑:好吧,这是松散的暗示,但我显然其他人亲自问我这个问题仍然发现它很不清楚.这是一个简单的Q + A,我提供的不是我实际上试图找到答案这个微不足道的问题)
我需要一只手来解决我的列字段的问题.我需要在这两个不同的"模式"字符串之间提取字符串,例如:
[... string] contract = 1234567890123350566076070666已发出= [string ...]
我想在'contract ='和'issued ='之间提取字符串
目前我正在使用
SELECT substring(substring_index(licence_key,'contract=',-1),1,40) FROM table
Run Code Online (Sandbox Code Playgroud)
问题是它之间的这个字符串不总是有40个字符,所以它不是固定的长度,因此它之前和之后的数据.这是一个不稳定的数据.
你知道我怎么能处理吗?
正如文档所述,使用regex.search(string, pos, endpos)并不完全等同于切割字符串,即regex.search(string[pos:endpos]).它不会像字符串开始那样进行正则表达式匹配pos,因此^不匹配子字符串的开头,而只匹配整个字符串的实际开头.但是,$匹配子字符串的结尾或整个字符串.
>>> re.compile('^am').findall('I am falling in code', 2, 12)
[] # am is not at the beginning
>>> re.compile('^am').findall('I am falling in code'[2:12])
['am'] # am is the beginning
>>> re.compile('ing$').findall('I am falling in code', 2, 12)
['ing'] # ing is the ending
>>> re.compile('ing$').findall('I am falling in code'[2:12])
['ing'] # ing is the ending
>>> re.compile('(?<= )am').findall('I am falling …Run Code Online (Sandbox Code Playgroud) 我(在SQLite数据库中)有以下字符串:
????? ? ??????? ?? ????? ????????, ??????? ? ???????????? ?????? ??????? ??????.
PHP使用正确显示该字符串print.我想获得这个字符串的前50个字符,即
????? ? ??????? ?? ????? ????????, ??????? ? ?????.
我尝试过使用substr和mb_substr,然后得到
????? ? ??????? ?? ????? ???,即只有28个字符.
在这里和其他地方读到mbstring的问题之后,我意识到这实际上是一个50字节的字符串(22个俄语字符= 44个字节加上5个空格加1个问号).
这有什么好的解决方案吗?我的所有字符串都是UTF-8,所以我当然可以自己编写一个子函数,通过检查每个字节的第一位等等.但这肯定是在之前完成的,对吧?
更新:我认为mb_substr无法正常工作,因为mb_detect_encoding() 无法正常工作.
我的任务是使用JavaScript将给定的数组拆分为更小的数组.例如[1, 2, 3, 4]应该拆分为[1] [1, 2] [1, 2, 3] [1, 2, 3, 4] [2] [2, 3] [2, 3, 4] [3] [3, 4] [4].
我正在使用此代码:
let arr = [1, 2, 3, 4];
for (let i = 1; i <= arr.length; i++) {
let a = [];
for (let j = 0; j < arr.length; j++) {
a.push(arr[j]);
if (a.length === i) {
break;
}
}
console.log(a);
}Run Code Online (Sandbox Code Playgroud)
我得到以下结果: [1] [1, 2] [1, 2, 3] [1, 2, 3, …