[英]How do I take the end off of a String in Java/Android?
I have a string that looks like this: 我有一个看起来像这样的字符串:
http%3A%2F%2Fwww.myurl.com%2Fbarcodes%2Fimages%2F024543634737.jpg
I just want the end of the string past the / (%2F) so I get the following: 我只希望字符串的结尾超过/(%2F),所以得到以下信息:
024543634737.jpg
Is there a RegEx or something that I can use with Java? 是否存在RegEx或我可以在Java中使用的东西? Can someone post some quick code?
有人可以发布一些快速代码吗?
String splitString = "/"; // you can change it to %2F
String s = "http://www.myurl.com/barcodes/images/024543634737.jpg";
int index = s.lastIndexOf(splitString);
String result= null;
if(index > -1){
result = s.substring(index+splitString.length());
}
Try the following (using String.lastIndexOf
and String.substring
methods) - 尝试以下操作(使用
String.lastIndexOf
和String.substring
方法)-
String input = "http%3A%2F%2Fwww.myurl.com%2Fbarcodes%2Fimages%2F024543634737.jpg";
System.out.println(input.substring(input.lastIndexOf("%2F") + 3));
Output: 输出:
024543634737.jpg
024543634737.jpg
you can use lastIndexOf() method of String class. 您可以使用String类的lastIndexOf()方法。
lastIndexOf()
Searches for the last occurrence of a character or substring. lastIndexOf()
搜索字符或子字符串的最后一次出现。 see like this, 看到这样
String str = "http%3A%2F%2Fwww.myurl.com%2Fbarcodes%2Fimages%2F024543634737.jpg";
int i = str.lastIndexOf("%");
String result = str.substring ( i );
String[] array= "http%3A%2F%2Fwww.myurl.com%2Fbarcodes%2Fimages%2F024543634737.jpg".split("%2F");
String own = array[array.length-1];
if(myString.indexOf("/")>=0){
myString = myString.substring(myString.lastIndexOf("/")+1, myString.length());
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.