C# 删除由 \ 分割的标签中的最后一个单词
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2155668/
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
remove last word in label split by \
提问by user175084
Ok i have a string where i want to remove the last word split by \
好的,我有一个字符串,我想在其中删除由 \ 分割的最后一个单词
for example:
例如:
string name ="kak\kdk\dd\ddew\cxz\"
now i want to remove the last word so that i get a new value for name as
现在我想删除最后一个单词,以便我获得 name 的新值
name= "kak\kdk\dd\ddew\"
name="kak\kdk\dd\ddew\"
is there an easy way to do this
是否有捷径可寻
thanks
谢谢
采纳答案by Webleeuw
How do you get this string in the first place? I assume you know that '\' is the escape character in C#. However, you should get far by using
首先你是如何得到这个字符串的?我假设您知道 '\' 是 C# 中的转义字符。但是,您应该使用
name = name.TrimEnd('\')
name = name.Remove(name.LastIndexOf('\') + 1);
回答by dtb
string result = string.Join("\",
"kak\kdk\dd\ddew\cxz\"
.Split(new[] { '\' }, StringSplitOptions.RemoveEmptyEntries)
.Reverse()
.Skip(1)
.Reverse()
.ToArray()) + "\";
回答by Nick Higgs
This regex replacement should do the trick:
这个正则表达式替换应该可以解决问题:
name = Regex.Replace(name, @"\[a-z]*\$", "\");
回答by Joel Etherton
Here's a non-regex manner of doing it.
这是一种非正则表达式方式。
string newstring = name.SubString(0, name.SubString(0, name.length - 1).LastIndexOf('\'));
回答by Rubens Farias
Try this:
尝试这个:
const string separator = "\";
string name = @"kak\kdk\dd\ddew\cxz\";
string[] names = name.Split(separator.ToCharArray(),
StringSplitOptions.RemoveEmptyEntries);
string result = String.Join(separator, names, 0, names.Length - 1) + separator;
回答by JeffH
EDIT:I just noticed that name.Substring(0,x)
is equivalent to name.Remove(x)
, so I've changed my answer to reflect that.
编辑:我刚刚注意到这name.Substring(0,x)
相当于name.Remove(x)
,所以我改变了我的答案以反映这一点。
In a single line:
在一行中:
name = name = name.Remove(name.Remove(name.Length - 1).LastIndexOf('\') + 1);
If you want to understand it, here's how it might be written out (overly) verbosely:
如果你想理解它,下面是它可能被(过度)冗长地写出来的方式:
string nameWithoutLastSlash = name.Remove(name.Length - 1);
int positionOfNewLastSlash = nameWithoutLastSlash.LastIndexOf('\') + 1;
string desiredSubstringOfName = name.Remove(positionOfNewLastSlash);
name = desiredSubstringOfName;
回答by Bhaskar
string name ="kak\kdk\dd\ddew\cxz\"
string newstr = name.TrimEnd(@"\")
回答by Siddarth Kanted
My Solution
我的解决方案
public static string RemoveLastWords(this string input, int numberOfLastWordsToBeRemoved, char delimitter)
{
string[] words = input.Split(new[] { delimitter });
words = words.Reverse().ToArray();
words = words.Skip(numberOfLastWordsToBeRemoved).ToArray();
words = words.Reverse().ToArray();
string output = String.Join(delimitter.ToString(), words);
return output;
}
Function call
函数调用
RemoveLastWords("kak\kdk\dd\ddew\cxz\", 1, '\')
回答by L'Code
if you working with paths:
如果您使用路径:
string name = @"kak\kdk\dd\ddew\cxz\";
Path.GetDirectoryName(name.TrimEnd('\'));
//ouput: kak\kdk\dd\ddew