简体   繁体   中英

using Func<string, bool>, how can I split on a character then count the length of those values within a function

Essentially, I need to split a string like "aaaaa.bbbbbbbb.cccccc" on the . and then count the length of the split values using a function.

Func<string, bool> length = f => f.Split(".").Length > 1;

pretty much this but instead of counting the length of the split array, I need to count how many letters per entry of the array and see if they are over a certain length.

If you need a boolean answer, then it's either one of the following (depdending on if you want at least one substring with length > 1 or you want all of them:

    Func<string, bool> length1 = f => f.Split('.').Any(s => s.Length > 1);
    Func<string, bool> length2 = f => f.Split('.').All(s => s.Length > 1);

I think you are trying to do this:

string input = "aaaaa.bbbbbbbb.cccccc";
var parts = input.Split('.');
var lengths = parts.Select(e=>e.Count());

如果您按字母表示:aaaaa.bbbbbbbbbb.cccccc-> a = 5,b = 8,c = 6

Func<string, IEnumerable<int>> length = f => f.Split('.').Select(a => a.Length)

From what I understand, you need to take a string, split it only by the delimiter '.', and then count to see if each split value is greater than 1.

If so, you can use the following:

Func<string, bool> length = str => str.Split('.').All(s => s.Length > 1);

This will first split the string by your delimiter, then iterate on all of the values to check if they are greater than 1.

Quick test case:

string test1 = "aaa.b.ccccccc";
string test2 = "aaaaaa.bbb.c";
string test3 = "aaa.bbb.ccccc";

Console.WriteLine(length(test1)); // false, as b is 1, not greater
Console.WriteLine(length(test2)); // false, for similar reasons
Console.WriteLine(length(test3)); // true, all are greater

what about this [Example reference] :

Func<string, List<int>> length = f => f.Split('.').Select(x=>x.Length).ToList();

And call the method like this:

string inputStr = "aaaaa.bbbbbbbb.cccccc";
Console.WriteLine(String.Join(",",length(inputStr))); // prints 5,8,6

Please note: the second parameter in the Func denotes the return type, Here in the example I used it as List<int> that's why I added .ToList() at the end of the code. You can change the return types accordingly. If you are ok with IEnumerable<int> then

Func<string, IEnumerable<int>> length = f => f.Split('.').Select(x=>x.Length) 

is enough.

Another way using a func delegate:

Func<string, Tuple<string[], int[]>> length = (str) => {
    string[] stringParts = str.Split('.');
    int[] countLetters = stringParts.Select(s => s.Length).ToArray();
    return new Tuple<string[], int[]>(stringParts, countLetters);
};

string input = "aaaaa.bbbbbbbb.cccccc";

var res = length(input);
string[] strs = res.Item1;
int[] countLetter = res.Item2;


for (int i = 0; i < strs.Length; i++)
{
    Console.WriteLine(strs[i]);
    Console.WriteLine(countLetter[i]);
}

Output:

aaaaa
5
bbbbbbbb
8
cccccc
6

An extension method would perhaps be an easier way to do this.

How about you write a method that returns list of strings that above the length you specify, something like this:

IEnumerable<string> GetSplittedAboveLimit(string inputString,int limit)
{
   var splitted = inputString.Split(".");
   foreach(var input in splitted)
   {
      if(input.Length > limit)
      {
          yield return input; 
      }
   }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM