C#使用Substring,如何提取此字符串?

时间:2021-09-26 07:11:11

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”,但文件夹可以具有任何名称并且可以是任意长度。考虑到这一点,我如何使用substring来提取第一个文件夹名称?

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.

这是我正在努力的长度。

7 个解决方案

#1


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]?

#2


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].

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

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

或者,您可以执行此操作来拼接foldername:

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

#3


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 :)

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

#4


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.

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

#5


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。

#6


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中完成的,如果是这样的话,我认为可能有另一种方法可以在不进行查询的情况下实现这一点。

#7


folderName.Split('/')[1]

#1


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]?

#2


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].

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

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

或者,您可以执行此操作来拼接foldername:

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

#3


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 :)

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

#4


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.

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

#5


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。

#6


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中完成的,如果是这样的话,我认为可能有另一种方法可以在不进行查询的情况下实现这一点。

#7


folderName.Split('/')[1]