2013-04-08 96 views
1

我有例如,这些线路的背后:字符串 - 得到的字符串中指定的子

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:串的每一行的一部分吗?

+0

谢谢解答! :-) – gaffcz 2013-04-08 13:32:12

+0

你应该使用正则表达式来减少你想要的分隔符,比如在这种情况下:'' 2013-04-08 13:18:40

回答

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
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 
+0

@dasblinkenlight谢谢。 – 2013-04-08 13:23:26

1
String result = new String(str.substring(0,str.indexOf("<text:"))); 
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); 
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()));

弗朗西斯