Как разделить строку при игнорировании регистра разделителя?

Существуют утилиты, которые сделают это для Вас.

В прошлом я использовал инструмент MS (depends.exe), который шел (я думаю), VB.:
https://msdn.microsoft.com/en-us/library/8kche8ah.aspx

и также существует это:
http://dependencywalker.com/

и вероятно другие также.

29
задан jdelator 16 September 2009 в 23:51
поделиться

5 ответов

There's no easy way to accomplish this using string.Split. (Well, except for specifying all the permutations of the split string for each char lower/upper case in an array - not very elegant I think you'll agree.)

However, Regex.Split should do the job quite nicely.

Example:

var parts = Regex.Split(input, "aa", RegexOptions.IgnoreCase);
65
ответ дан 28 November 2019 в 00:54
поделиться

If you don't care about case, then the simplest thing to do is force the string to all uppercase or lowercase before using split.

stringbits = datastring.ToLower().Split("aa")

If you care about case for the interesting bits of the string but not the separators then I would use String.Replace to force all the separators to a specific case (upper or lower, doesn't matter) and then call String.Split using the matching case for the separator.

strinbits = datastring.Replace("aA", "aa").Replace("AA", "aa").Split("aa")
5
ответ дан 28 November 2019 в 00:54
поделиться

In your algorithm, you can use the String.IndexOf method and pass in OrdinalIgnoreCase as the StringComparison parameter.

5
ответ дан 28 November 2019 в 00:54
поделиться

It's not the pretties version but also works:

"asdf aA asdfget aa uoiu AA".Split(new[] { "aa", "AA", "aA", "Aa" }, StringSplitOptions.RemoveEmptyEntries);
2
ответ дан 28 November 2019 в 00:54
поделиться

My answer isn't as good as Noldorin's, but I'll leave it so people can see the alternative method. This isn't as good for simple splits, but it is more flexible if you need to do more complex parsing.

using System.Text.RegularExpressions;

string data = "asdf aA asdfget aa uoiu AA";
string aaRegex = "(.+?)[aA]{2}";

MatchCollection mc = Regex.Matches(data, aaRegex);

foreach(Match m in mc)
{
    Console.WriteLine(m.Value);
}
4
ответ дан 28 November 2019 в 00:54
поделиться
Другие вопросы по тегам:

Похожие вопросы: