C# 使用子字符串,我如何提取这个字符串?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/439255/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 03:26:42  来源:igfitidea点击:

C# Using Substring, how do I extract this string?

c#substring

提问by

I want to extract the first folder in the URL below, in this example it is called 'extractThisFolderName' but the folder could have any name and be any length. With this in mind how can I use substring to extract the first folder name?

我想提取下面 URL 中的第一个文件夹,在这个例子中它被称为“extractThisFolderName”,但该文件夹可以有任何名称和任何长度。考虑到这一点,我如何使用子字符串来提取第一个文件夹名称?

The string: www.somewebsite.com/extractThisFolderName/leave/this/behind

字符串:www.somewebsite.com/extractThisFolderName/leave/this/behind

String folderName = path.Substring(path.IndexOf(@"/"),XXXXXXXXXXX);

It's the length I'm struggling with.

这是我挣扎的长度。

回答by Daniel Schaffer

If you're getting a Uri, why not just do uri.Segments[0]?

如果你得到一个 Uri,为什么不直接做 uri.Segments[0]?

Or even path.Split(new Char[] { '/' })[1]?

甚至path.Split(new Char[] { '/' })[1]

回答by Sam Meldrum

int start = path.IndexOf('/');
int end = path.IndexOf('/', start + 1);
if (end == -1) end = path.Length;
string folderName = path.Substring(start + 1, end - start - 1);

EDIT: Daniel Schaffer's answer about using uri segments is preferable, but left this in as it may be your path is not really a valid uri.

编辑:Daniel Schaffer 关于使用 uri 段的回答是可取的,但保留它,因为它可能是您的路径不是真正有效的 uri。

回答by NilObject

If you're going to be using each path part, you can use:

如果您要使用每个路径部分,您可以使用:

String[] parts = path.Split('/');

At which point you can access the "extractThisFolderName" part by accessing parts[1].

此时您可以通过访问parts[1] 来访问“extractThisFolderName”部分。

Alternatively, you can do this to splice out the foldername:

或者,您可以这样做来拼接文件夹名称:

int firstSlashIndex = path.IndexOf('/');
int secondSlashIndex = path.IndexOf('/', firstSlashIndex + 1);
String folderName = path.Substring(firstSlashIndex + 1, secondSlashIndex - firstSlashIndex);

回答by JoshBerke

You could do:

你可以这样做:

string myStr = "www.somewebsite.com/extractThisFolderName/leave/this/behind";
int startIndex = myStr.IndexOf('/') + 1;
int length = myStr.IndexOf('/', startIndex) - startIndex;
Console.WriteLine(myStr.Substring(startIndex, length));

At the same point I assume this is being done in ASP.Net if so I think there might be another way to get this without doign the querying.

同时,我假设这是在 ASP.Net 中完成的,如果是这样的话,我认为可能有另一种方法可以在不进行查询的情况下获得它。

回答by Jon Skeet

Daniel's answer gives you other practical ways of doing it. Another alternative using substring:

丹尼尔的回答为您提供了其他实用的方法。使用子字符串的另一种选择:

int start = path.IndexOf('/')+1; // Note that you don't need a verbatim string literal
int secondSlash = path.IndexOf('/', start);
return path.Substring(start, secondSlash-start);

You'll want to add some error checking in there, of course :)

你当然想在那里添加一些错误检查:)

回答by devio

folderName.Split('/')[1]

回答by PEZ

The problem also lends itself to regular expressions. An expression like:

这个问题也适用于正则表达式。像这样的表达式:

(?<host>.*?)/(?<folder>.*?)/

Is clear about what's going on and you can get the data out by those names.

很清楚发生了什么,您可以通过这些名称获取数据。