在java中,我想重命名一个String,所以它总是以“.mp4”结尾
假设我们有一个编码链接,如下所示:
String link = www.somehost.com/linkthatIneed.mp4?e=13974etc...
那么,如何重命名link
字符串,使其始终以“.mp4”结束?
link = www.somehost.com/linkthatIneed.mp4 <--- that's what I need the final String to be.
答案 0 :(得分:2)
答案 1 :(得分:1)
另一种方法是将split
带有“.mp4”的字符串作为拆分字符然后再添加:)
类似的东西:
String splitChar = ".mp4";
String link = "www.somehost.com/linkthatIneed.mp4?e=13974etcrezkhjk"
String finalStr = link.split(splitChar)[0] + splitChar;
容易做^^
PS:我更喜欢通过正则表达式,但它要求更多关于正则表达式的知识^^
答案 2 :(得分:0)
答案 3 :(得分:0)
尝试以下代码,
private String trimStringAfterOccurance(String link, String occuranceString) {
Integer occuranceIndex = link.indexOf(occuranceString);
String trimmedString = (String) link.subSequence(0, occuranceIndex + occuranceString.length() );
System.out.println(trimmedString);
return trimmedString;
}