C# 从字符串中删除最后一个正斜杠和尾随字符的简写方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12936804/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Shorthand way to remove last forward slash and trailing characters from string
提问by Curt
If I have the following string:
如果我有以下字符串:
/lorem/ipsum/dolor
/lorem/ipsum/dolor
and I want this to become:
我希望这变成:
/lorem/ipsum
/lorem/ipsum
What is the short-hand way of removing the last forward slash, and all characters following it?
删除最后一个正斜杠及其后的所有字符的简写方法是什么?
I know how I can do this by spliting the string into a List<>and removing the last item, and then joining, but is there a shorter way of writing this?
我知道如何通过将字符串拆分为 aList<>并删除最后一个项目然后加入来做到这一点,但是有没有更短的写法?
My question is notURL specific.
我的问题不是特定于 URL 的。
采纳答案by newfurniturey
You can use Substring()and LastIndexOf():
您可以使用Substring()和LastIndexOf():
str = str.Substring(0, str.LastIndexOf('/'));
EDIT(suggested comment)
To prevent any issues when the string may not contain a /, you could use something like:
编辑(建议评论)
为了防止字符串可能不包含 a 时出现任何问题/,您可以使用以下内容:
int lastSlash = str.LastIndexOf('/');
str = (lastSlash > -1) ? str.Substring(0, lastSlash) : str;
Storing the position in a temp-variable would prevent the need to call .LastIndexOf('/')twice, but it could be dropped in favor of a one-line solution instead.
将位置存储在临时变量中可以避免调用.LastIndexOf('/')两次的需要,但可以将其删除以支持单行解决方案。
回答by Sjoerd
You can do something like str.Remove(str.LastIndexOf("/")), but there is no built-in method to do what you want.
你可以做类似的事情str.Remove(str.LastIndexOf("/")),但没有内置的方法来做你想做的事。
Edit: you could also use the Uri object to traverse directories, although it does not give exactly what you want:
编辑:您也可以使用 Uri 对象来遍历目录,尽管它没有给出您想要的内容:
Uri baseUri = new Uri("http://domain.com/lorem/ipsum/dolor");
Uri myUri = new Uri(baseUri, ".");
// myUri now contains http://domain.com/lorem/ipsum/
回答by lc.
You can use the regex /[^/]*$and replace with the empty string:
您可以使用正则表达式/[^/]*$并替换为空字符串:
var fixed = new Regex("/[^/]*$").Replace("domain.com/lorem/ipsum/dolor", "")
But it's probably overkill here. @newfurniturey's answer of Substringwith LastIndexOfis probably best.
但这里可能有点矫枉过正。@newfurniturey 对Substringwith的回答LastIndexOf可能是最好的。
回答by BugFinder
One simple way would be
一种简单的方法是
String s = "domain.com/lorem/ipsum/dolor";
s = s.Substring(0, s.LastIndexOf('/'));
Console.WriteLine(s);
Another maybe
另一个也许
String s = "domain.com/lorem/ipsum/dolor";
s = s.TrimEnd('/');
Console.WriteLine(s);
回答by Mason240
while (input.Last() == '/' || input.Last() == '\')
{
input = input.Substring(0, input.Length - 1);
}
回答by ehuna
I like to create a String Extension for stuff like this:
我喜欢为这样的东西创建一个字符串扩展:
/// <summary>
/// Returns with suffix removed, if present
/// </summary>
public static string TrimIfEndsWith(
this string value,
string suffix)
{
return
value.EndsWith(suffix) ?
value.Substring(0, value.Length - suffix.Length) :
value;
}
You can then use like this:
然后你可以像这样使用:
var myString = "/lorem/ipsum/dolor";
myStringClean = myString.TrimIfEndsWith("/dolor");
You now have a re-usable extension across all of your projects that can be used to remove one trailing character or multiple.
您现在在所有项目中都有一个可重复使用的扩展,可用于删除一个或多个尾随字符。
回答by Xin
If there is '/' at the end of the url, remove it. If not; just return the original one.
如果网址末尾有“/”,请将其删除。如果不; 只返回原始的。
var url = this.Request.RequestUri.ToString();
url = url.EndsWith("/") ? url.Substring(0, url.Length - 1) : url;
url += @"/mycontroller";
回答by KazKazar
Thank you @Curt for your question. I slightly improved @newfurniturey's code, and here is my version.
谢谢@Curt 的提问。我稍微改进了@newfurniturey 的代码,这是我的版本。
if(str.Contains('/')){
str = str.Substring(0, str.LastIndexOf('/'));
}

