开发者

StringBuffer find 'start' and 'end' and write anything between them

StringBuffer find 'start' and 'end' and write anything between th开发者_JS百科em

example:

text1
text2
start
text3
text4
end
text5

can i just print out of StringBuffer text3 and text4' Text that is between 'start' and 'end'


Assumming you have a StringBuffer sb that already contains the text:

int start = sb.indexOf("start");
int end = sb.indexOf("end");
if (start != -1 && end != -1) {
    String fragment = sb.substring(start + "start".length(), end);
    System.out.println(fragment);
}

Note that this finds text between "start" and "end" as you asked for in your question. However if the text in the StringBuffer has line breaks, you might actually want to search for text between "start\n" and "end\n".


use indexOf(String str) method to find out the index of start & end

Doc

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜