如何在忽略分隔符的情况下拆分字符串?

时间:2022-03-26 09:27:10

I need to split a string let's say "asdf aA asdfget aa uoiu AA" split using "aa" ignoring the case. to

我需要拆分一个字符串让我们说“asdf aA asdfget aa uoiu AA”拆分使用“aa”忽略这个案例。至

"asdf "
"asdfget "
"uoiu "

6 个解决方案

#1


53  

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

使用string.Split没有简单的方法来实现这一点。 (好吧,除了为数组中的每个字符大小写/大写字母指定拆分字符串的所有排列 - 不是很优雅我认为你会同意。)

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

但是,Regex.Split应该可以很好地完成这项工作。

Example:

例:

var parts = Regex.Split(input, "aa", RegexOptions.IgnoreCase);

#2


5  

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.

如果您不关心大小写,那么最简单的方法是在使用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.

如果您关心字符串的有趣位而不是分隔符的情况,那么我将使用String.Replace强制所有分隔符到特定的大小写(大写或小写,无关紧要),然后调用String.Split使用匹配分隔符的大小写。

strinbits = datastring.Replace("aA", "aa").Replace("AA", "aa").Split("aa")

#3


5  

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

在您的算法中,您可以使用String.IndexOf方法并将OrdinalIgnoreCase作为StringComparison参数传递。

#4


4  

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.

我的答案不如Noldorin那么好,但我会留下它让人们可以看到替代方法。这对于简单的拆分不太好,但如果您需要进行更复杂的解析,它会更灵活。

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);
}

#5


2  

It's not the pretties version but also works:

这不是pretties版本,但也有效:

"asdf aA asdfget aa uoiu AA".Split(new[] { "aa", "AA", "aA", "Aa" }, StringSplitOptions.RemoveEmptyEntries);

#6


1  

    public static List<string> _Split(this string input,string[] splt)
    {
        List<string> _Result=new List<string>();
        foreach(string _splt in splt)
        {
            if (splt.Count() == 1)
            { 
                _Result.AddRange(Regex.Split(input, _splt, RegexOptions.IgnoreCase).ToList());
            }
            else 
            {
                List<string> NewStr = Regex.Split(input, _splt, RegexOptions.IgnoreCase).ToList();
                foreach(string _NewStr in NewStr)
                {
                    List<string> NewSplt = splt.ToList();
                    NewSplt.Remove(_splt);
                    return _Split(_NewStr, NewSplt.ToArray());
                }
            } 
        }
        return _Result;
    } 

then use this function as bellow

然后使用此功能如下所示

public frmThematicConversation()
{
    InitializeComponent();
    string str = "a b c d e f g h a b c f a d c b f";
    string[] splt = { "a", "b" };
    List<string> _result = str._Split(splt);
}

#1


53  

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

使用string.Split没有简单的方法来实现这一点。 (好吧,除了为数组中的每个字符大小写/大写字母指定拆分字符串的所有排列 - 不是很优雅我认为你会同意。)

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

但是,Regex.Split应该可以很好地完成这项工作。

Example:

例:

var parts = Regex.Split(input, "aa", RegexOptions.IgnoreCase);

#2


5  

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.

如果您不关心大小写,那么最简单的方法是在使用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.

如果您关心字符串的有趣位而不是分隔符的情况,那么我将使用String.Replace强制所有分隔符到特定的大小写(大写或小写,无关紧要),然后调用String.Split使用匹配分隔符的大小写。

strinbits = datastring.Replace("aA", "aa").Replace("AA", "aa").Split("aa")

#3


5  

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

在您的算法中,您可以使用String.IndexOf方法并将OrdinalIgnoreCase作为StringComparison参数传递。

#4


4  

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.

我的答案不如Noldorin那么好,但我会留下它让人们可以看到替代方法。这对于简单的拆分不太好,但如果您需要进行更复杂的解析,它会更灵活。

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);
}

#5


2  

It's not the pretties version but also works:

这不是pretties版本,但也有效:

"asdf aA asdfget aa uoiu AA".Split(new[] { "aa", "AA", "aA", "Aa" }, StringSplitOptions.RemoveEmptyEntries);

#6


1  

    public static List<string> _Split(this string input,string[] splt)
    {
        List<string> _Result=new List<string>();
        foreach(string _splt in splt)
        {
            if (splt.Count() == 1)
            { 
                _Result.AddRange(Regex.Split(input, _splt, RegexOptions.IgnoreCase).ToList());
            }
            else 
            {
                List<string> NewStr = Regex.Split(input, _splt, RegexOptions.IgnoreCase).ToList();
                foreach(string _NewStr in NewStr)
                {
                    List<string> NewSplt = splt.ToList();
                    NewSplt.Remove(_splt);
                    return _Split(_NewStr, NewSplt.ToArray());
                }
            } 
        }
        return _Result;
    } 

then use this function as bellow

然后使用此功能如下所示

public frmThematicConversation()
{
    InitializeComponent();
    string str = "a b c d e f g h a b c f a d c b f";
    string[] splt = { "a", "b" };
    List<string> _result = str._Split(splt);
}