没有MiddleIndexOf
,但两者都IndexOf
允许LastIndexOf
您选择开始搜索的点。
例如:
string str = @"1\2\3\4\5";
// indexes: 012345678
int index = str.IndexOf('\\', 4);
// index = 5, it starts searching from: "3\4\5"
要获取第二个斜杠的索引,您需要执行以下操作:
string str = @"1\2\3\4\5";
int index = str.IndexOf('\\', str.IndexOf('\\') + 1);
要获得倒数第二个斜杠,代码如下:
string str = @"1\2\3\4\5";
int index = str.LastIndexOf('\\', str.LastIndexOf('\\') - 1);
要列出字符串中字符的每个索引,这些扩展方法可以提供帮助:
public static int[] GetAllIndexes(this string @this, string substr)
{
var indexes = new List<int>();
int index = -1;
while ((index = @this.IndexOf('\\', index + 1)) >= 0) indexes.Add(index);
return indexes.ToArray();
}
public static int[] GetAllIndexes(this string @this, char substr)
{
var indexes = new List<int>();
int index = -1;
while ((index = @this.IndexOf('\\', index + 1)) >= 0) indexes.Add(index);
return indexes.ToArray();
}
它可以这样使用:
string str = @"1\2\3\4\5";
int[] indexes = str.GetAllIndexes('\\');
您描述的BeforeLastIndexOf
功能可能如下所示:
public static int BeforeLastIndexOf(this string @this, string substr, int beforeCount)
{
int index = @this.LastIndexOf(substr, @this.Length - 1);
for (int i = 0; i < beforeCount && index >= 0; i++)
index = @this.LastIndexOf(substr, index - 1);
return index;
}
public static int BeforeLastIndexOf(this string @this, char substr, int beforeCount)
{
int index = @this.LastIndexOf(substr, @this.Length - 1);
for (int i = 0; i < beforeCount && index >= 0; i++)
index = @this.LastIndexOf(substr, index - 1);
return index;
}
你会像这样使用它:
string str = @"1\2\3\4\5";
// indexes: 0123456789
int index = str.BeforeLastIndexOf('\\', 2);
它会在最后一个索引之前返回 2 个索引。在这种情况下,它会返回 3。(\5 是最后一个,然后是 \4,然后是 \3,\3 之前的 \ 的索引是 3)。
你可以做同样的事情IndexOf
并做一个AfterIndexOf
:
public static int AfterIndexOf(this string @this, string substr, int afterCount)
{
int index = @this.IndexOf(substr);
for (int i = 0; i < afterCount && index >= 0; i++)
index = @this.IndexOf(substr, index + 1);
return index;
}
public static int AfterIndexOf(this string @this, char substr, int afterCount)
{
int index = @this.IndexOf(substr);
for (int i = 0; i < afterCount && index >= 0; i++)
index = @this.IndexOf(substr, index + 1);
return index;
}
你可以像这样使用它:
string str = @"1\2\3\4\5";
// indexes: 0123456789
int index = str.AfterIndexOf('\\', 2);
这又返回 5,即 4 之前的 \。
要真正获得 MiddleIndexOf,或者换句话说,最接近中间的索引,您可以使用此扩展方法:
public static int MiddleIndexOf(this string @this, string substr, bool roundUp = false, bool preferUp = true)
{
// Determine the middle character
int middlePoint = (roundUp ? @this.Length : @this.Length - 1) / 2;
// Find the indexes closest to the middle
int indexBelow = @this.LastIndexOf(substr, middlePoint);
int indexAbove = @this.IndexOf(substr, middlePoint);
if (indexBelow < 0) return indexAbove;
if (indexAbove < 0) return indexBelow;
int diffBelow = middlePoint - indexBelow;
int diffAbove = indexAbove - middlePoint;
return diffAbove == diffBelow ? (preferUp ? indexAbove : indexBelow) : // If it's the same difference
(diffAbove < diffBelow ? indexAbove : indexBelow); // Otherwise return the closest index
}
public static int MiddleIndexOf(this string @this, char substr, bool roundUp = false, bool preferUp = true)
{
// Determine the middle character
int middlePoint = (roundUp ? @this.Length : @this.Length - 1) / 2;
// Find the indexes closest to the middle
int indexBelow = @this.LastIndexOf(substr, middlePoint);
int indexAbove = @this.IndexOf(substr, middlePoint);
if (indexBelow < 0) return indexAbove;
if (indexAbove < 0) return indexBelow;
int diffBelow = middlePoint - indexBelow;
int diffAbove = indexAbove - middlePoint;
return diffAbove == diffBelow ? (preferUp ? indexAbove : indexBelow) : // If it's the same difference
(diffAbove < diffBelow ? indexAbove : indexBelow); // Otherwise return the closest index
}
你可以像这样使用它:
string str = @"1\2\3\4\5\";
int index = str.MiddleIndexOf('\\');
有两个可选参数:
roundUp
: 如果它设置为 true,并且你有偶数个字符,它会向上取整,否则它会向下取整。例如,如果您有 8 个字符:如果 roundUp 为 false,"12345678"
它将选择作为中间点,否则.4
5
preferUp
:当字符串后面的中间点与字符串前面的字符一样多的字符时,这将确定它选择哪个字符。默认情况下,它设置为true
补偿向下舍入。(如果你有一个 8 个字符"\1\2\3\4"
的字符串,中间点应该是 3.5,但是因为这是不可能的,所以它会从索引 3 开始搜索。索引 3与上面一样远。但由于这是'2'
真的\
,它会选择索引 4 处距离中点 0.5 远的字符,否则它会选择下面的索引,这将1.5
远离真正的中点。)