Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/56.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从indexof(word)到下一行的子字符串_Java_Android_String_Substring - Fatal编程技术网

Java 从indexof(word)到下一行的子字符串

Java 从indexof(word)到下一行的子字符串,java,android,string,substring,Java,Android,String,Substring,我希望子字符串从indexof(“&”)一直到下一行,我的代码是 String substring = thisSrch.srchdesc.substring(thisSrch.srchdesc.indexOf("&"), frstNextNewLine); 但是我不知道怎样才能找到新的frstNextNewLine 对于exmaple,我希望在下面的文本中检测“&阿里是第一个加入的人。” 一种美妙的宁静占据了我的整个灵魂,就像 我全心全意地享受着这些春天甜美的

我希望子字符串从
indexof(“&”)
一直到下一行,我的代码是

  String substring = thisSrch.srchdesc.substring(thisSrch.srchdesc.indexOf("&"), frstNextNewLine);
但是我不知道怎样才能找到新的
frstNextNewLine
对于exmaple,我希望在下面的文本中检测“&阿里是第一个加入的人。

一种美妙的宁静占据了我的整个灵魂,就像 我全心全意地享受着这些春天甜美的早晨

&阿里是第一个加入的人。

我独自一人,在这一点上感受到存在的魅力,那就是 为我这样的灵魂创造幸福

请试试这个

String substring;
for(int i=0;i<thisSrch.srchdesc.length;i++){
    if(thisSrch.srchdesc.indexOf("\n")>thisSrch.srchdesc.indexOf("&")){
      substring = thisSrch.srchdesc.substring(thisSrch.srchdesc.indexOf("&"), thisSrch.srchdesc.indexOf("\n"));
      break;
    }
}
请试试这个

String substring;
for(int i=0;i<thisSrch.srchdesc.length;i++){
    if(thisSrch.srchdesc.indexOf("\n")>thisSrch.srchdesc.indexOf("&")){
      substring = thisSrch.srchdesc.substring(thisSrch.srchdesc.indexOf("&"), thisSrch.srchdesc.indexOf("\n"));
      break;
    }
}
你试过这个吗

substring.split("\n")[0]
你试过这个吗

substring.split("\n")[0]
上面使用的indexOf函数接受一个参数,开始在“&”字符的索引后寻找新行

上面使用的indexOf函数接受一个参数,以开始在“&”字符的索引后查找新行。

srchdesc.substring(thisrch.srchdesc.indexOf(“&”)、thisrch.srchdesc.indexOf(“\n”、thisrch.srchdesc.indexOf(“&”)+1))

这里substring函数将返回带有两个传递索引的子字符串

您传递的第一个参数是正确的。 必须传递的第二个参数是indexOf(“\n”) 这里\n标识新行,indexOf中的第二个参数是您要搜索的索引“\n”之后的索引“&”,因此它将返回“&”之后下一次出现的“\n”的索引(“&”

srchdesc.substring(thisrch.srchdesc.indexOf(&)”,thisrch.srchdesc.indexOf(“\n”,thisrch.srchdesc.indexOf(“&”)+1))

这里substring函数将返回带有两个传递索引的子字符串

您传递的第一个参数是正确的。 必须传递的第二个参数是indexOf(“\n”)
此处\n标识新行。indexOf中的第二个参数是您要搜索的索引“\n”之后的索引,这将是您的“&”索引,因此它将返回“&”之后下一次出现的“\n”索引。

post here your string@ArMo 372i添加了示例,表示您必须检测。或/n?但在“&阿里是第一个加入的人。行”之前和之后是否有多个“\n”在这里张贴您的字符串@ArMo 372i添加了示例,表示您必须检测。或/n?但在“&Ali是第一个加入.line的人”之前和之后是多个“\n”但在“&Ali是第一个加入.line的人”之前和之后是多个“\n”但在“&Ali是第一个加入.line的人”之前和之后是多个“\n”吗