IndexOf
,IndexOfAny
和LastIndexOf
,LastIndexOfAny
似乎不这样做(或者可能他们这样做)。我正在寻找std :: string的find_first_not_of
和find_last_not_of
的equialent。我正在考虑创建一个扩展类,但我不确定C#是否已经提供了这个功能。C#相当于C++ std :: string find_first_not_of和find_last_not_of
回答
string source = "the quick brown fox jumps over the lazy dog";
string chars = "ogd hte";
int? firstNotOf = source.Select((x, i) => new { Val = x, Idx = (int?)i })
.Where(x => chars.IndexOf(x.Val) == -1)
.Select(x => x.Idx)
.FirstOrDefault();
int? lastNotOf = source.Select((x, i) => new { Val = x, Idx = (int?)i })
.Where(x => chars.IndexOf(x.Val) == -1)
.Select(x => x.Idx)
.LastOrDefault();
或者,如果你喜欢一些非LINQ扩展方法。这些应该有稍好的性能,特别是对于FindLastNotOf
:
int? firstNotOf = source.FindFirstNotOf(chars);
int? lastNotof = source.FindLastNotOf(chars);
// ...
public static int? FindFirstNotOf(this string source, string chars)
{
if (source == null) throw new ArgumentNullException("source");
if (chars == null) throw new ArgumentNullException("chars");
if (source.Length == 0) return null;
if (chars.Length == 0) return 0;
for (int i = 0; i < source.Length; i++)
{
if (chars.IndexOf(source[i]) == -1) return i;
}
return null;
}
public static int? FindLastNotOf(this string source, string chars)
{
if (source == null) throw new ArgumentNullException("source");
if (chars == null) throw new ArgumentNullException("chars");
if (source.Length == 0) return null;
if (chars.Length == 0) return source.Length - 1;
for (int i = source.Length - 1; i >= 0; i--)
{
if (chars.IndexOf(source[i]) == -1) return i;
}
return null;
}
(这有可能是你可能获得更好的性能 - 在两个LINQ和非LINQ版本 - 如果转换chars
到HashSet<char>
,或者甚至可能是一个简单的char[]
阵列,你需要进行基准测试才能发现,尽管除非chars
变得相当大,否则任何差异都可能忽略不计。)
如果使用LINQ是可以接受的,可以用适当的谓词调用First()和Last()方法。
举例来说,如果你要的是不是元音的第一个和最后一个字符:
string vowels = "aeiouy";
char first = yourString.First(ch => vowels.IndexOf(ch) < 0);
char last = yourString.Last(ch => vowels.IndexOf(ch) < 0);
编辑:以上将返回字符,而不是他们的索引。为了做到这一点,你可以用Select()技术的项目指标,但事情会变得毛茸茸的,因为我们需要回到-1
如果没有字符匹配:
int firstIndex = (yourString.Select(
(ch, i) => new { Character = ch, Index = i }
).First(obj => vowels.IndexOf(obj.Character) < 0)
?? new { Character = '\0', Index = -1 }).Index;
int lastIndex = (yourString.Select(
(ch, i) => new { Character = ch, Index = i }
).Last(obj => vowels.IndexOf(obj.Character) < 0)
?? new { Character = '\0', Index = -1 }).Index;
另外,这里的基础上@ abatishchev的一个不太复杂的解决方案回答:
string vowels = "aeiouy";
int firstIndex = yourString.IndexOf(yourString.First(
ch => vowels.IndexOf(ch) < 0));
int lastIndex = yourString.LastIndexOf(yourString.Last(
ch => vowels.IndexOf(ch) < 0));
不完全:'find_first_not_of'返回位置,而不是字符。 – Vlad 2010-12-21 10:58:55
@Vlad,你是对的,回答相应更新。谢谢:) – 2010-12-21 11:06:22
我认为,LINQ过分复杂的东西。而这个解决方案是非常无效的。我们与迭代器一起创建了很多匿名类型的临时对象。对于循环来说简单的书写方法会更好。 – 2010-12-21 11:15:29
这是一个正则表达式解决方案。
string testString = "oueytestie";
var matchFirstNotOf = Regex.Match(testString, @"[^aeiouy]");
int firstNotOf = matchFirstNotOf.Success ? matchFirstNotOf.Index : -1;
var matchLastNotOf = Regex.Match(testString, @"[^aeiouy]", RegexOptions.RightToLeft);
int lastNotOf = matchLastNotOf.Success ? matchLastNotOf.Index : -1;
- 1. C++ std :: string和string
- 2. Cython C++和std :: string
- 3. 使用std :: sscanf和std :: string的C++
- 4. STL相关seg错误在C++ std :: string?
- 5. C++循环std :: vector <std :: map <std :: string,std :: string>>
- 6. C/C++相当于C#System.Net.Mail
- 7. std :: string name和std :: string&name之间的C++区别
- 8. 相当于C#
- 9. C#相当于fprintf
- 10. C#相当于pydoc
- 11. C#相当于OBJ-C的
- 12. C#相当于InStrRev
- 13. C#相当于FREAD
- 14. C++的std :: string构造
- 15. C++ /串连的std :: string和WCHAR *至* WCHAR
- 16. C#ULONG_PTR相当于
- 17. C++ std :: string到Ruby VALUE
- 18. C++:的std :: string问题
- 19. C++ std :: string到布尔值
- 20. 在C#中的std :: string?
- 21. C++ const std :: string赋值
- 22. C++相当于tailq
- 23. boost :: format表单c-string或std :: string
- 24. C++/CX:将std :: string转换为Platform :: String^
- 25. C-pstree相当于
- 26. httplib相当于C
- 27. strtoul相当于C#
- 28. C++相当于java.lang.Integer.MIN_VALUE
- 29. ungetc相当于c#
- 30. std :: string和stdarg.h
不错的工作,但是他们正确的头脑在图书馆或API程序中发现任何可以接受的东西?看起来像一个MS失败。 – GTAE86 2014-03-28 18:11:13