【问题标题】:String - get string behind the specified substringString - 获取指定子字符串后面的字符串
【发布时间】:2013-03-30 14:12:35
【问题描述】:
例如,我有以下几行:
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: 字符串后面每一行的部分?
【问题讨论】:
标签:
java
string
substring
【解决方案1】:
像这样:
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。
【解决方案2】:
String result = new String(str.substring(0,str.indexOf("<text:")));
【解决方案3】:
@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】:
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
【解决方案5】:
试试这个:
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()));
弗朗切斯科