使用Java如何从字符串“ADACADABRA”中提取“CADA”?
此链接中如何提取“/”和“?”之间的ID。 输出应为:zaaU9lJ34c5,但在过程中应使用“/”和“?”。
此链接中如何提取“/”和“?”之间的ID。 输出应为:zaaU9lJ34c5,但在过程中应使用“/”和“?”。
and also how to extract the id between "/" and "?" from the link below.
http://www.youtube-nocookie.com/embed/zaaU9lJ34c5?rel=0
output should be: zaaU9lJ34c5
应该是:
String url = "http://www.youtube-nocookie.com/embed/zaaU9lJ34c5?rel=0";
String str = url.substring(url.lastIndexOf("/") + 1, url.indexOf("?"));
String s = "ADACADABRA";
String s2 = s.substring(3,7);
这里的3指定了起始索引,7指定了停止点。返回的字符串包含从起始索引开始的所有字符,但不包括结束索引。
我不完全确定您所说的“extract”的意思,因此我提供了从字符串中删除它的代码,我不确定这是否是您想要的。
public static void main (String args[]){
String string = "ADACADABRA";
string = string.replace("CADA", "");
System.out.println(string);
}
String youtubeUrl = "http://www.youtube-nocookie.com/embed/zaaU9lJ34c5?rel=0";
String[] urlParts = youtubeUrl.split("/");
String videoId = urlParts[urlParts.length - 1];
videoId = videoId.substring(0, videoId.indexOf("?"));
String regex = "CADA";
Pattern p = Pattern.compile(regex, Pattern.MULTILINE);
Matcher m = p.matcher(originalText);
while (m.find()) {
String outputThis = m.group(1);
}
可能你没有考虑到 java.lang.String
的不可变性。这就是为什么你需要将子字符串的结果分配给一个新变量。