[英]Java: Getting a substring from a string starting after a particular character
I have a string:我有一个字符串:
/abc/def/ghfj.doc
I would like to extract ghfj.doc
from this, ie the substring after the last /
, or first /
from right.我想从中提取ghfj.doc
,即最后一个/
之后的子字符串,或从右边开始的第一个/
。
Could someone please provide some help?有人可以提供一些帮助吗?
String example = "/abc/def/ghfj.doc";
System.out.println(example.substring(example.lastIndexOf("/") + 1));
A very simple implementation with String.split()
:一个非常简单的String.split()
:
String path = "/abc/def/ghfj.doc";
// Split path into segments
String segments[] = path.split("/");
// Grab the last segment
String document = segments[segments.length - 1];
what have you tried?你试过什么? it's very simple:这很简单:
String s = "/abc/def/ghfj.doc";
s.substring(s.lastIndexOf("/") + 1)
Another way is to use this .另一种方法是使用this 。
String path = "/abc/def/ghfj.doc"
String fileName = StringUtils.substringAfterLast(path, "/");
If you pass null to this method it will return null.如果您将 null 传递给此方法,它将返回 null。 If there is no match with separator it will return empty string.如果与分隔符不匹配,它将返回空字符串。
This can also get the filename这也可以获得文件名
import java.nio.file.Paths;
import java.nio.file.Path;
Path path = Paths.get("/abc/def/ghfj.doc");
System.out.println(path.getFileName().toString());
Will print ghfj.doc
将打印ghfj.doc
String id="/abc/def/ghfj.doc";
String valIfSplitIsEmpty="";
return Iterables.getLast(Splitter.on("/").split(id),valIfSplitIsEmpty);
Eventually configure the Splitter
and use最终配置Splitter
并使用
Splitter.on("/")
.trimResults()
.omitEmptyStrings()
...
Also take a look into this article on guava Splitter and this article on guava Iterables还可以看看 这篇关于 guava Splitter 的文章和这篇关于 guava Iterables 的文章
In Kotlin you can use substringAfterLast
, specifying a delimiter.在 Kotlin 中,您可以使用substringAfterLast
,指定一个分隔符。
val string = "/abc/def/ghfj.doc"
val result = url.substringAfterLast("/")
println(result)
// It will show ghfj.doc
Returns a substring after the last occurrence of delimiter.返回最后一次出现分隔符后的子字符串。 If the string does not contain the delimiter, returns missingDelimiterValue which defaults to the original string.如果字符串不包含分隔符,则返回默认为原始字符串的 missingDelimiterValue。
I think that would be better if we use directly the split function我认为如果我们直接使用 split 函数会更好
String toSplit = "/abc/def/ghfj.doc";
String result[] = toSplit.split("/");
String returnValue = result[result.length - 1]; //equals "ghfj.doc"
java android爪哇机器人
in my case就我而言
I want to change from我想从
~/propic/........png ~/propic/.........png
anything after /propic/ doesn't matter what before it /propic/之后的任何内容都无关紧要
........png .......png
finally, I found the code in Class StringUtils最后,我在Class StringUtils 中找到了代码
this is the code这是代码
public static String substringAfter(final String str, final String separator) {
if (isEmpty(str)) {
return str;
}
if (separator == null) {
return "";
}
final int pos = str.indexOf(separator);
if (pos == 0) {
return str;
}
return str.substring(pos + separator.length());
}
static String substringAfterLast(String all, String part) {
return all.substring(all.lastIndexOf(part) + part.length() + 1);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.