String - 获取指定子字符串后面的字符串

时间:2013-04-08 13:15:45

标签: java string substring

我有这些行:

af asf af dassfdfsdf a dfa sd<text:text which i need to store
xycCYXCYXCaAdadad<text:text which i need to store
56afdasfaaaaaa54554 ewrt<text:text which i need to store

如何将每行的部分放在<text:字符串后面?

5 个答案:

答案 0 :(得分:4)

像这样:

String line = "af asf af dassfdfsdf a dfa sd<text:text which i need to store";
int pos = line.indexOf("<text:");
if (pos > 0) {
    line = line.substring(pos+6); // 6 is the length of your "<text:" marker
}

这是demo on ideone

答案 1 :(得分:1)

public static void main(String[] args) {
    String str = "af asf af dassfdfsdf a dfa sd<text:text which i need to store";
    String result = str.substring(str.indexOf("<text:")+"<text:".length());
    System.out.println(result);
}

输出:

text which i need to store

答案 2 :(得分:1)

String result = new String(str.substring(0,str.indexOf("<text:")));

答案 3 :(得分:1)

@gaffcz,....

Try below code once

String str = "af asf af dassfdfsdf a dfa sd<text:text";
int lastIndex =     str.lastIndexOf("<text:text");
str = str.substring(0, lastIndex);
System.out.println("str : "+str);

答案 4 :(得分:1)

试试这个:

String a ="af asf af dassfdfsdf a dfa sd<text:text"; System.out.println(a.subSequence(0, a.lastIndexOf("<text:text") != -1 ? a.lastIndexOf("<text:text"): a.length()));

弗朗西斯