我有一个网址,我希望它看起来像这样:
Action Manatee - Action
http://xxxxxx.com/songs2/Music%20Promotion/Stream/Action%20Manatee%20-%20Action.mp3
修剪到“Stream /”之后的位置的语法是什么,并创建%20所在的空格。我也想修剪.mp3
答案 0 :(得分:0)
嗯,对于那个特例,我会根据'/'字符拆分字符串然后修剪最后''后面的文字。字符。最后,将“%20”替换为“”。这应该留给你想要的字符串
String initial = "http://xxxxxx.com/songs2/Music%20Promotion/Stream/Action%20Manatee%20-%20Action.mp3";
String[] split = initial.split("/");
String output = split[split.length-1];
int length = output.lastIndexOf('.');
output = output.substring(0, length);
output = output.replace("%20", " ");
答案 1 :(得分:0)
您可以使用split()
和replace(
)方法来实现此目的,这里有两种方法:
使用正斜杠将您的字符串拆开:
string yourUrl = [URL Listed];
//Breaks your URL into sections on slashes
string[] sections = yourUrl.split("\/");
//Grabs the last section after the slashes, and replaces the %20 with spaces
string newString = sections[sectiongs.length-1].replace("%20"," ");
在Stream /部分分割您的字符串:(如果您可以保证它将采用该格式,则仅使用此内容)
string yourUrl = [URL Listed];
//This will get everything after Stream (your song name)
string newString = yourUrl.split("Stream\/")[1];
//Replaces your %20s with spaces
newString = newString.replace("%20"," ");
答案 2 :(得分:0)
String urlParts[] = URL.split("\/");
String urlLast = urlParts[length-1];
String nameDotMp = urlLast.replaceAll("%20");
String name = nameDotMp.substring(0,nameDotMp.length-5);
答案 3 :(得分:0)
URL songURL = new URL("yourpath/filename");
String filename = songURL.getFile();